Thinking outside the box

Patron Saint of Lost Yaks
posts - 203, comments - 734, trackbacks - 4

My Links

Advertisement

News

Archives

Post Categories

Getting date or time only from a Datetime value

SELECT GETDATE() AS theFullDateTime,
       DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) AS theDateOnly,
       DATEADD(DAY, DATEDIFF(DAY, GETDATE(), 0), GETDATE()) AS theTimeOnly

SELECT GETDATE() AS theFullDateTime,
       DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()), '19000101') AS theDateOnly,
       DATEADD(DAY, DATEDIFF(DAY, GETDATE(), '19000101'), GETDATE()) AS theTimeOnly

Print | posted on Wednesday, November 12, 2008 8:38 AM | Filed Under [ Optimization SQL Server 2008 Algorithms SQL Server 2005 SQL Server 2000 ]

Feedback

Gravatar

# re: Getting date or time only from a Datetime value

SELECT GETDATE() AS fullDateTime,
CONVERT(VARCHAR, GETDATE(), 101) AS dateOnly,
CONVERT(VARCHAR, GETDATE(), 108) AS timeOnly
11/12/2008 9:12 PM | Bill Curnow
Gravatar

# re: Getting date or time only from a Datetime value

The basic idea is to keep the values as datetimes, even if only time is preserved.

And you should use style 114 instead if 108.
11/12/2008 10:15 PM | Peso
Gravatar

# re: Getting date or time only from a Datetime value

"Should" is largely subjective, but yes, 114 preserves the entire time right down to milliseconds.
11/12/2008 11:29 PM | Bill Curnow
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET