sqlrx logo
SQLRx® Tip of the Month
 
 
February 2010
Greetings!
 
Check out the functions below, and don't forget to monitor the 11 VitalSigns used by SQLRx to debug performance bottlenecks.
SQL 2008SQL Server T-SQL:  Working with time values in queries has always presented challenges, especially when you need only part of a datetime value. Either truncating or rounding time has usually been done by converting a datetime value to a string and then using the parts that are needed. The functions below can be used to return time in several different states. Use them to round or truncate time values.  
 
 

CREATE FUNCTION dbo.fnRoundTimeToNearestSecond (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN DATEADD(ms,ROUND(DATEDIFF(ms,DATEADD(day,DATEDIFF(day,0,@TimeIn),0),@TimeIn ),-3),DATEADD(day,DATEDIFF(day,0,@TimeIn),0))

END

GO

-------------------------------------------------

 

CREATE FUNCTION dbo.fnRoundTimeToNearestMinute (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN dbo.fnRoundTimeToMinIncrement(@TimeIn, 1)

END

GO

------------------------------------------------

 

CREATE FUNCTION dbo.fnTruncateTimeToNearestHour (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN DATEADD(Hour, DATEDIFF(Hour, 0, @TimeIn), 0)

END

GO

------------------------------------------------

 

CREATE FUNCTION dbo.fnTruncateTimeToNearestDay (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN DATEADD(Day, DATEDIFF(Day, 0, @TimeIn), 0)

END

GO

------------------------------------------------

 

CREATE FUNCTION dbo.fnTruncateTimeToNearestMonth (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN DATEADD(Month, DATEDIFF(Month, 0, @TimeIn), 0)

END

GO

------------------------------------------------

 

CREATE FUNCTION dbo.fnTruncateTimeToNearestYear (@TimeIn DATETIME)

RETURNS DATETIME

AS

BEGIN

      RETURN DATEADD(Year, DATEDIFF(Year, 0, @TimeIn), 0)

END

GO

------------------------------------------------

 

CREATE FUNCTION dbo.fnRoundTimeToMinIncrement

(

        @TIME DATETIME

      , @MIN SMALLINT

)

RETURNS DATETIME

AS

BEGIN

      SET @MIN = ABS(@MIN) -- Ensure minutes is a positive number

      IF @MIN > 1440

            SET @MIN = 1440

 

       -- We are going to separate the DATE and TIME parts, do some math and add them together

       RETURN DATEADD(DAY, 0, DATEDIFF(DAY, 0, @TIME)) + -- Here's the DATE part

                  CAST(ROUND(CAST(CAST(CONVERT(VARCHAR(12), @TIME, 14) AS DATETIME) AS FLOAT) * 1440.0 / @MIN, 0) / (1440.0 / @MIN) AS DATETIME) -- And now the TIME math

END

GO

__________________________________________________________________ 
SQLRx is now a part of www.msdev.com, Microsoft's training site for solution providers!  Check the site periodically to view new training videos.
 
If you have a SQL question you'd like us to answer, and possibly use for the next SQLRx Tip of the Month or msdev video, email it to us!  If you missed any of our previous tips, you can view them here.
 
For assistance or more information on optimizing your SQL Server Environment, visit us at www.sqlrx.com or email Lori Brown at lorib@isi85.com.  

Sincerely,

Lori Brown | SQLRx Senior Consultant | Integrated Services, Inc.
4144 N. Central Expwy, Suite 430  |  Dallas, TX  75204
Phone: 214.526.7680 x 113 | lorib@isi85.com 
 
 
Integrated Services, Inc.                                    
2008 winner logo small
Turning Data Into Profit
 
Over 20 Years of Credibility & Trust
 

Join our email list          Check out our events          SQLRx Downloads