Robin Hames

Hints, tricks and tips relating to MS SQL Server and .NET
posts - 14 , comments - 44 , trackbacks - 0

Tuesday, August 11, 2009

Getting the Time part only from a SQL Server DateTime

Just some code to get the time part only from a DateTime field (is SQL Server 2000 or 2005), with the date set to a reference date

 

declare @timetest datetime

declare @refdate datetime

 

set @timetest = getdate()

set @refdate = '30 Dec 1899 00:00:00'

 

select @timetest

      , @refdate

      , dateadd(day, datediff(day, @refdate , @timetest), @refdate ) -- date part only

      , dateadd(day, datediff(day, @refdate , @timetest) * -1, @timetest) -- time part only, with reference date

 

Instead of @refdate, you could use 0.

 

Posted On Tuesday, August 11, 2009 12:24 PM | Comments (0) | Filed Under [ SQL Server ]

Powered by: