﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Luis Atondo  / GetDateTimeString Function / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 10:34:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>Tks to all, it's realy better performance with your suggestions</description><pubDate>Fri, 31 Jul 2009 11:10:43 GMT</pubDate><dc:creator>Atd</dc:creator></item><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>[quote][b]DCDBA (7/31/2009)[/b][hr]How about this:CREATE FUNCTION dbo.fGetDateTimeString(  @dt datetime)RETURNS varchar(12)ASBEGIN  RETURN Convert(varchar(8), @dt, 112) + Replace(Convert(varchar(5), @dt, 114), ':', '')END[/quote]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.</description><pubDate>Fri, 31 Jul 2009 09:26:09 GMT</pubDate><dc:creator>SQLNightOwl</dc:creator></item><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>How about this:CREATE FUNCTION dbo.fGetDateTimeString(  @dt datetime)RETURNS varchar(12)ASBEGIN  RETURN Convert(varchar(8), @dt, 112) + Replace(Convert(varchar(5), @dt, 114), ':', '')END</description><pubDate>Fri, 31 Jul 2009 08:31:30 GMT</pubDate><dc:creator>DCDBA</dc:creator></item><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>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)</description><pubDate>Fri, 31 Jul 2009 05:47:12 GMT</pubDate><dc:creator>Ol'SureHand</dc:creator></item><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>An Alternative:[code]ALTER FUNCTION [dbo].[GetDateTimeString] (	@inDateTime datetime)RETURNS varchar(12)ASBEGIN	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 @DateStringEND[/code]</description><pubDate>Fri, 31 Jul 2009 04:47:27 GMT</pubDate><dc:creator>simon.duckett</dc:creator></item><item><title>RE: GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>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)ASBEGIN	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 @DateStringEND</description><pubDate>Fri, 31 Jul 2009 04:29:36 GMT</pubDate><dc:creator>simon.duckett</dc:creator></item><item><title>GetDateTimeString Function</title><link>http://www.sqlservercentral.com/Forums/Topic759564-1350-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Date+Time/67549/"&gt;GetDateTimeString Function&lt;/A&gt;[/B]</description><pubDate>Fri, 24 Jul 2009 19:50:07 GMT</pubDate><dc:creator>Atd</dc:creator></item></channel></rss>