Robin Hames

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

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.

 

Print | posted on Tuesday, August 11, 2009 12:24 PM | Filed Under [ SQL Server ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: