Wednesday, November 04, 2009

MS SQL Military and Standard Time Format


There are times that you want to display non-Military or AM/PM time format. The standard Convert formats do not have an option to return the time in this format. The SQL below will return only the time portion of the current date/time in the AM/PM format.



SELECT substring(convert(varchar(20), GetDate(), 9), 13, 5) + ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2)<br />


However, when you want to retrieve the military time format:



SELECT CONVERT(CHAR(10),GETDATE(),108)



You can replace the GetDate function with any datetime field to return the time portion of that field.

No comments: