71

I have one Table which has two fields such as "StartTime" and "EndTime". The DataType of the Two columns are Time.

So the Values of the Table looks like as follows:

TableA:

            StartTime                EndTime
       ------------------         ----------------
        17:30:00.0000000          17:57:00.0000000

But I need the result as

            StartTime                EndTime
       ------------------         ----------------
            05:30 PM                 05:57 PM

When I select the table. How to get time in AM PM Format?

16 Answers 16

109

In SQL 2012 you can use the Format() function.

https://technet.microsoft.com/en-us/library/hh213505%28v=sql.110%29.aspx

Skip casting if the column type is (datetime).

Example:

SELECT FORMAT(StartTime,'hh:mm tt') AS StartTime
FROM TableA
0
90

Use following syntax to convert a time to AM PM format.

Replace the field name with the value in following query.

select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),100)

Output: 5:30PM

Better option is available with Sql 2012. First parameter should be of datetime data type.

DECLARE @d DATETIME = '10/01/2011 13:14';
SELECT FORMAT(@d,'hh:mm tt') 

Output : 01:14 PM

14
  • 4
    How can we add the space between minute and AM/PM? Commented Jan 21, 2014 at 20:01
  • 3
    It's not pretty, but here's one way you can add the space: SELECT REPLACE(REPLACE(CONVERT(varchar(15), CAST('17:30:00.0000000' AS TIME), 100), 'P', ' P'), 'A', ' A') Commented Jul 9, 2015 at 13:22
  • 1
    @hurleystylee, they may be many other indirect ways to put space before AM/PM and your solution is from one of them. Thanks for putting this. Commented Jul 10, 2015 at 7:59
  • 1
    @RahulHendawe Try declare @a smallint =17; select CONVERT(varchar(15),CAST(cast (@a as varchar(2)) + ':0:0' AS TIME),100) Commented Feb 2, 2017 at 13:19
  • 1
    @RahulHendawe 24:00:00 is not a valid time, so it's giving error. It should be 00:00:00 :) Commented Feb 3, 2017 at 6:51
19

Here are the various ways you may pull this (depending on your needs).

Using the Time DataType:

DECLARE @Time Time = '15:04:46.217'
SELECT --'3:04PM'
       CONVERT(VarChar(7), @Time, 0),

       --' 3:04PM' --Leading Space.
       RIGHT(' ' + CONVERT(VarChar(7), @Time, 0), 7),

       --' 3:04 PM' --Space before AM/PM.
       STUFF(RIGHT(' ' + CONVERT(VarChar(7), @Time, 0), 7), 6, 0, ' '),

       --'03:04 PM' --Leading Zero.  This answers the question above.
       STUFF(RIGHT('0' + CONVERT(VarChar(7), @Time, 0), 7), 6, 0, ' ')

       --'03:04 PM' --This only works in SQL Server 2012 and above.  :)
       ,FORMAT(CAST(@Time as DateTime), 'hh:mm tt')--Comment out for SS08 or less.

Using the DateTime DataType:

DECLARE @Date DateTime = '2016-03-17 15:04:46.217'
SELECT --' 3:04PM' --No space before AM/PM.
       RIGHT(CONVERT(VarChar(19), @Date, 0), 7),

       --' 3:04 PM' --Space before AM/PM.
       STUFF(RIGHT(CONVERT(VarChar(19), @Date, 0), 7), 6, 0, ' '),

       --'3:04 PM' --No Leading Space.
       LTRIM(STUFF(RIGHT(CONVERT(VarChar(19), @Date, 0), 7), 6, 0, ' ')),

       --'03:04 PM' --Leading Zero.
       STUFF(REPLACE(RIGHT(CONVERT(VarChar(19), @Date, 0), 7), ' ', '0'), 6, 0, ' ')

       --'03:04 PM' --This only works in SQL Server 2012 and above.  :)
       ,FORMAT(@Date, 'hh:mm tt')--Comment line out for SS08 or less.
3
  • What if I only want PM or AM to be displayed?
    – niq
    Commented Mar 3, 2017 at 5:26
  • STUFF is much better than Format Commented Jan 16, 2019 at 9:48
  • @abdulqayyum Agreed. I've taken huge performance hits before with FORMAT(), so now I avoid it altogether. It's convenience, but at too steep a cost. You never know how these things may affect a query in the future. It may run fine on a small set now, but years later it'll be choking on a few thousand of records. Source: Format is nice and all, but... and Beware the Performance Impact of the T-SQL Format Function
    – MikeTeeVee
    Commented May 11, 2021 at 0:26
8

This returns like 11:30 AM

select CONVERT(VARCHAR(5), FromTime, 108) + ' ' + RIGHT(CONVERT(VARCHAR(30), FromTime, 9),2)
from tablename
1
  • 2
    This works okay; however you also get results like 14:00 PM Commented Jul 9, 2015 at 13:20
4
SELECT CONVERT(varchar, StartTime, 100) AS ST,
       CONVERT(varchar, EndTime, 100) AS ET
FROM some_table

or

SELECT RIGHT('0'+ LTRIM(RIGHT(CONVERT(varchar, StartTime, 100),8)),8) AS ST,
       RIGHT('0'+ LTRIM(RIGHT(CONVERT(varchar, EndTime, 100),8)),8) AS ET
FROM some_table
2
  • Cleanest solution.
    – Kahless
    Commented Dec 3, 2017 at 5:38
  • 1
    this adds an extra 0 when hour has 2 digits (10-12) 010:40AM
    – RoLYroLLs
    Commented Apr 4, 2018 at 14:41
3
> SELECT CONVERT(VARCHAR(30), GETDATE(), 100) as date_n_time
> SELECT CONVERT(VARCHAR(20),convert(time,GETDATE()),100) as req_time
> select convert(varchar(20),GETDATE(),103)+' '+convert(varchar(20),convert(time,getdate()),100)

> Result (1):- Jun  9 2018 11:36AM
> result(2):-  11:35AM
> Result (3):-  06/10/2018 11:22AM
3

select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),100)

almost works perfectly except for the space issue. if that were changed to:

select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),22)

...then you get the space. And additionally, if the column being converted is already of TIME format, you can skip the cast if you reference it directly.

Final answer:

select CONVERT(varchar(15),StartTime,22)
1

Multiple functions, but this will give you what you need (tested on SQL Server 2008)

Edit: The following works not only for a time type, but for a datetime as well.

SELECT SUBSTRING(CONVERT(varchar(20),StartTime,22), 10, 11) AS Start, SUBSTRING(CONVERT(varchar(20),EndTime,22), 10, 11) AS End FROM [TableA];

0
1

Try this:

select CONVERT(VARCHAR(5), ' 4:07PM', 108) + ' ' + RIGHT(CONVERT(VARCHAR(30), ' 4:07PM', 9),2) as ConvertedTime
1
DECLARE @temp varchar(50)
set @temp = convert(varchar(max),STUFF(STUFF('645P',2,0,':'),5,0,' '))
select @temp

SELECT CASE
        WHEN RIGHT(@temp,1)='P' THEN REPLACE(@temp,'p','PM')
        WHEN RIGHT(@temp,1)='A' THEN REPLACE(@temp,'a','AM')
        END
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 9, 2023 at 21:05
0

Try this:

select CONVERT(varchar(15),CAST('2014-05-28 16:07:54.647' AS TIME),100) as CreatedTime
0
    select right(convert(varchar(20),getdate(),100),7)
1
  • Could you explain your answer?
    – hardkoded
    Commented Jun 16, 2017 at 18:34
0

Using @Saikh's answer above, the 2nd option, you can add a space between the time itself and the AM or PM.

REVERSE(LEFT(REVERSE(CONVERT(VARCHAR(20),CONVERT(TIME,myDateTime),100)),2) + ' ' + SUBSTRING(REVERSE(CONVERT(VARCHAR(20),CONVERT(TIME,myDateTime),100)),3,20)) AS [Time],

Messy I know, but it's the solution I chose. Strange that the CONVERT() doesn't add that space automatically. SQL Server 2008 R2

0

One option is to use the FORMAT() function like this:

-- Returns "03:15 AM"
DECLARE @iniTme TIME = '03:15:30'
SELECT FORMAT(CAST(@iniTme AS DATETIME),  'hh:mm tt')

-- Returns "03:15 PM"
DECLARE @endTme TIME = '15:15:30'
SELECT FORMAT(CAST(@endTme AS DATETIME),  'hh:mm tt')
1
  • 1
    You need to add some explanation to your answer
    – Joseph
    Commented May 3, 2022 at 1:15
0

I wonder why the code below was not mentioned by anyone.

select  FORMAT(CAST(@Your_time_data_type AS datetime),'hh:mm tt') AS TimeStr

eg. select FORMAT(CAST('12:30' AS datetime),'hh:mm tt') AS TimeIn

0
    SELECT CONVERT(VARCHAR(20),GETDATE(),106)+' '+CONVERT(VARCHAR(20),CONVERT(TIME,GETDATE()),100)

-> RESULT : 30 Jan 2023 3:02PM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.