Wednesday, July 25, 2012

How to return the date part and time part from a SQL Server datetime datatype?

Introduction
                I am showing you how to Extract the date and Time from the current Date Time.

SELECT SUBSTRING(CONVERT(varchar(24), GetDate(), 121), 0, 11) as 'Date'

Date
2012-07-25

select SUBSTRING(CONVERT(varchar(24),GetDate(),121),12,12) as 'Time'

Time
01:45:43.133

If you want to Convert time to AM and PM Format then use Following querry

SELECT substring(convert(varchar(20), GetDate(), 9), 13, 5) + ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2) 'Time'

Time
1:45 AM

0 comments:

Post a Comment