Home  »  Code Snippets  »  SQL Server  »

Convert time from 24 hours format to 12 hours(AM PM) format in SQL server

 Posted By : Manoranjan SahooPosted Date : 13/05/2011 02:08:21 PM Category : SQL Server Points : 2

Use Below code to convert the time from 24 hours format to 12 hours format.

Suppose you have one column in a table with varchar field and store time in that field like 13:00 or 17:23

But at the time of retrieving the data you want to fetch with 12 hours format like 1:00 PM or 5:23 PM

Then use below code :

SELECT SUBSTRING(CONVERT(varchar, (convert(datetime,timecol)), 100), 13, 2) + ':'
+ SUBSTRING(CONVERT(varchar,(convert(datetime,timecol)), 100), 16, 2) + ' '
+ SUBSTRING(CONVERT(varchar, (convert(datetime,timecol)), 100), 18, 2) AS MyTime from MyTable

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.