Convert Time DataType into AM PM Format:

2020-02-16 23:53发布

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?

12条回答
劳资没心,怎么记你
2楼-- · 2020-02-17 00:26

Try this:

select CONVERT(VARCHAR(5), ' 4:07PM', 108) + ' ' + RIGHT(CONVERT(VARCHAR(30), ' 4:07PM', 9),2) as ConvertedTime
查看更多
淡お忘
3楼-- · 2020-02-17 00:27
select case 
      when DATEPART(hour,'17:30:00.0000000') > 12 
      then cast((DATEPART(hour,'17:30:00.0000000') - 12) as varCHAR(2)) + ':' + cast(DATEPART(minute,'17:30:00.0000000') as varCHAR(2)) + ' PM'
      else cast(DATEPART(hour,'17:30:00.0000000') as varCHAR(2)) + ':' + cast(DATEPART(minute,'17:30:00.0000000') as varCHAR(2)) + ' PM'
   end
查看更多
祖国的老花朵
4楼-- · 2020-02-17 00:37

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.
查看更多
干净又极端
5楼-- · 2020-02-17 00:37
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
查看更多
三岁会撩人
6楼-- · 2020-02-17 00:37

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

查看更多
劫难
7楼-- · 2020-02-17 00:41
> 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
查看更多
登录 后发表回答