Asked By Gerard
26-Jun-07 09:28 AM

Hi,
Because I'm using sp_add_jobschedule in some of my code (stored proc)
I came up with the function below so that a user who wants to add a
job schedule for a given date & time does not have to do the
conversion from date or time to an int.
It may all sound a bit superfluous but not all of our staff here are
well versed at SQL but may need to call on the stored proc (which is
in essence a wrapper around sp_add_job, sp_add_jobschedule etc) from
their own code.
I look forward to your feedback on the workings and structure of the
function and any problems you may spot with it.
thanks.
Gerard
___________________________________________
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
/****** Object: User Defined Function dbo.ingrFnDateTimeInt Script
Date: 20-6-2007 15:37 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].
[ingrFnDateTimeInt]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[ingrFnDateTimeInt]
GO
CREATE FUNCTION ingrFnDateTimeInt (@Date datetime, @ReturnType
char(1))
RETURNS int
AS
BEGIN
/*
This function returns an integer value for either date or time
based
upon the combination of the date and return type provided. The
format
of the integer value is:
* hhmmss, so 07:00:00 returns 7000
* yyyymmdd, so 1971/07/30 returns 19710730
If no valid return type is provided (D or d for date and T or t for
time)
the value returned by this function is zero.
*/
DECLARE
@DateTimeInt int,
@cYear char(4),
@cMonth char(2),
@cDay char(2),
@cHours char(2),
@cMins char(2),
@cSecs char(2)
IF(@ReturnType = 'd' or @ReturnType = 'D')
BEGIN
SET @cYear = RTRIM(CAST(DATEPART(yyyy, @Date) as char(4)))
IF(LEN(RTRIM(CAST(DATEPART(m, @Date) as char(2))))) = 1
BEGIN
SET @cMonth = '0' + RTRIM(CAST(DATEPART(m, @Date) as char(2)))
END
ELSE
BEGIN
SET @cMonth = RTRIM(CAST(DATEPART(m, @Date) as char(2)))
END
IF(LEN(RTRIM(CAST(DATEPART(d, @Date) as char(2))))) = 1
BEGIN
SET @cDay = '0' + RTRIM(CAST(DATEPART(d, @Date) as char(2)))
END
ELSE
BEGIN
SET @cDay = RTRIM(CAST(DATEPART(d, @Date) as char(2)))
END
SET @DateTimeInt = CONVERT(int, @cYear + @cMonth + @cDay)
END
ELSE IF(@ReturnType = 't' or @ReturnType = 'T')
BEGIN
IF(LEN(RTRIM(CAST(DATEPART(hh, @Date) as char(2))))) = 1
BEGIN
SET @cHours = '0' + RTRIM(CAST(DATEPART(hh, @Date) as char(2)))
END
ELSE
BEGIN
SET @cHours = RTRIM(CAST(DATEPART(hh, @Date) as char(2)))
END
IF(LEN(RTRIM(CAST(DATEPART(n, @Date) as char(2))))) = 1
BEGIN
SET @cMins = '0' + RTRIM(CAST(DATEPART(n, @Date) as char(2)))
END
ELSE
BEGIN
SET @cMins = RTRIM(CAST(DATEPART(n, @Date) as char(2)))
END
IF(LEN(RTRIM(CAST(DATEPART(s, @Date) as char(2))))) = 1
BEGIN
SET @cSecs = '0' + RTRIM(CAST(DATEPART(s, @Date) as char(2)))
END
ELSE
BEGIN
SET @cSecs = RTRIM(CAST(DATEPART(s, @Date) as char(2)))
END
SET @DateTimeInt = CONVERT(int, @cHours + @cMins + @cSecs)
END
ELSE
BEGIN
SET @DateTimeInt = 0
END
RETURN(@DateTimeInt)
END
GO
GRANT EXECUTE ON ingrFnDateTimeInt TO DYNGRP
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO