search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
SQL Server GroupsView
SQL Server Ce
SQL Server Clients
SQL Server Clustering
SQL Server Connect
SQL Server Dts
SQL Server Fulltext
SQL Server Integrationsvcs
SQL Server Msde
SQL Server Newusers
SQL Server Olap
SQL Server Programming
SQL Server Replication
SQL Server Reportingsvcs
SQL Server Security
SQL Server Server
SQL Server Setup
SQL Server Tools
SQL Server Xml

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Application Development
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft SQL Server Programming Posts  Ask A New Question 

UDF - your comments please - Gerard

Tuesday, June 26, 2007 9: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
reply
 

UDF - your comments please - Tibor Karaszi

Tuesday, June 26, 2007 9:43 AM

Gerard,

It seems that http://sqldev.net/sqlagent/SQLAgentDateTime.htm does something similar, and might have
some other handy conversions already done for you...?

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
reply

Or you could use the ones Gert Drapers (high-up in the SQL team at ) - Aaron Bertrand [SQL Server MVP]

Tuesday, June 26, 2007 9:43 AM

Or you could use the ones Gert Drapers (high-up in the SQL team at
Microsoft) wrote for exactly this purpose:
http://www.sqldev.net/sqlagent/SQLAgentDateTime.htm

--
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
reply

Simplifies to something like this... - markc60

Tuesday, June 26, 2007 9:49 AM

Simplifies to something like this...

CREATE FUNCTION ingrFnDateTimeInt (@Date datetime, @ReturnType
char(1))
RETURNS INT
AS
BEGIN
RETURN CASE WHEN @ReturnType='D' THEN CAST(CONVERT(char(8),@Date,112)
AS INT)
WHEN @ReturnType='T' THEN
CAST(REPLACE(CONVERT(char(8),@Date,108),':','') AS INT)
ELSE 0 END
END
reply

Mark, thanks for the insights. - Gerard

Friday, June 29, 2007 9:00 AM

Mark, thanks for the insights. It becomes a very sweet little function
like this.

Aaron and Tibor, thanks for the urls

Regards,
Gerard
reply

Previous Microsoft SQL Server Programming conversation.