Caveman's Blog

My commitment to learning.

SQL Server: Get only the date and/or only the time part

leave a comment »


In this post I want to highlight some very useful Microsoft SQL Server System functions that can used to fetch datetime, only the date and only the time.

Fetch various date and times


SELECT SYSDATETIME() [DATE TIME]
,SYSDATETIMEOFFSET() [DATE TIME OFFSET]
,SYSUTCDATETIME() [UTC DATE TIME]
,CURRENT_TIMESTAMP [CURRENT_TIMESTAMP]
,GETDATE() [DATE]
,GETUTCDATE() [UTC DATE];

Fetch only the date part


SELECT CONVERT (date, SYSDATETIME()) [DATE]
,CONVERT (date, SYSDATETIMEOFFSET()) [DATE OFFSET]
,CONVERT (date, SYSUTCDATETIME()) [UTC DATE]
,CONVERT (date, CURRENT_TIMESTAMP) [CURRENT DATESTAMP]
,CONVERT (date, GETDATE()) [DATE]
,CONVERT (date, GETUTCDATE()) [UTC DATE];

Fetch only the date part


SELECT CONVERT (time, SYSDATETIME()) [SYS TIME]
,CONVERT (time, SYSDATETIMEOFFSET()) [ TIME OFFSET]
,CONVERT (time, SYSUTCDATETIME()) [UTC TIME]
,CONVERT (time, CURRENT_TIMESTAMP) [CURRENT_TIMESTAMP]
,CONVERT (time, GETDATE()) [TIME]
,CONVERT (time, GETUTCDATE()) [UTC TIME];


					
Advertisements

Written by cavemansblog

June 21, 2012 at 8:28 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: