Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


GetDateTimeString Function


GetDateTimeString Function

Author
Message
Atd
Atd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 84
Comments posted to this topic are about the item GetDateTimeString Function
simon.duckett
simon.duckett
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
I would construct a function like this for the same purpose.
Not so easy to read but I think it is probably a little more efficient.

/*
DECLARE @DTest NVARCHAR(14)
SET @DTest=dbo.GetDateTimeString(getdate())
SELECT @DTest as Test,getdate()
*/

ALTER FUNCTION [dbo].[GetDateTimeString]
(
@inDateTime datetime
)
RETURNS varchar(12)
AS
BEGIN
DECLARE @DateString varchar(12)

SELECT @DateString = CONVERT(varchar(8), @inDateTime, 112) +
STUFF('00', 3 - LEN(CONVERT(varchar(2), DATEPART(hour, @inDateTime))), 2, CONVERT(varchar(2), DATEPART(hour, @inDateTime))) +
STUFF('00', 3 - LEN(CONVERT(varchar(2), DATEPART(minute, @inDateTime))), 2, CONVERT(varchar(2), DATEPART(minute, @inDateTime)))
RETURN @DateString

END
simon.duckett
simon.duckett
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 99
An Alternative:


ALTER FUNCTION [dbo].[GetDateTimeString]
(
@inDateTime datetime
)
RETURNS varchar(12)
AS
BEGIN
DECLARE @DateString varchar(12), @Hours varchar(2), @Minutes varchar(2)

SELECT @Hours = CONVERT(varchar(2), DATEPART(hour, @inDateTime)),
@Minutes = CONVERT(varchar(2), DATEPART(minute, @inDateTime)),
@DateString = CONVERT(varchar(8), @inDateTime, 112) +
STUFF('00', 3 - LEN(@Hours), 2, @Hours) +
STUFF('00', 3 - LEN(@Minutes), 2, @Minutes)
RETURN @DateString

END


Ol'SureHand
Ol'SureHand
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 720
Good work. Most certainly there are cases when a string - or bigint - needs to be constructed this way.
Otherwise, of course, just the 112 format mask for CONVERT will produce a string that can be further cast as int.

A word of advice about UDF usage (this one being a tempting candidate!):
- indiscriminate use of the function will slow down queries and may end up in a limbo during multiple selects in views. The native CONVERT function is preferable (where a YYYYMMDD value is sufficient).
- use in stored procs is preferable, as a CONST before processing (avoid using it in-line)
DCDBA
DCDBA
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 85
How about this:

CREATE FUNCTION dbo.fGetDateTimeString
(
@dt datetime
)
RETURNS varchar(12)
AS
BEGIN
RETURN Convert(varchar(8), @dt, 112) + Replace(Convert(varchar(5), @dt, 114), ':', '')
END
SQLNightOwl
SQLNightOwl
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 503
DCDBA (7/31/2009)
How about this:

CREATE FUNCTION dbo.fGetDateTimeString
(
@dt datetime
)
RETURNS varchar(12)
AS
BEGIN
RETURN Convert(varchar(8), @dt, 112) + Replace(Convert(varchar(5), @dt, 114), ':', '')
END




DCDBA is the winner.

I ran the original code vs the modified code. There are a total of 12 steps and when looking at the execution plan each step takes 8.333% of the execution time (100 / 12).

So 8.333 * 11 = 91.663% for the original
vs.
8.334% for the modified.

It appears the author needs to press F1 and look up the CONVERT funciton.

--Paul Hunter
Atd
Atd
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 84
Tks to all, it's realy better performance with your suggestions
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10030 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search