Convert Time DataType into AM PM Format:

后端 未结 12 628
悲哀的现实
悲哀的现实 2020-12-23 16:02

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 follo

相关标签:
12条回答
  • 2020-12-23 16:27

    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 讨论(0)
  • 2020-12-23 16:35
    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
    
    0 讨论(0)
  • 2020-12-23 16:37

    Try this:

    select CONVERT(VARCHAR(5), ' 4:07PM', 108) + ' ' + RIGHT(CONVERT(VARCHAR(30), ' 4:07PM', 9),2) as ConvertedTime
    
    0 讨论(0)
  • 2020-12-23 16:40

    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)
    
    0 讨论(0)
  • 2020-12-23 16:45
        select right(convert(varchar(20),getdate(),100),7)
    
    0 讨论(0)
  • 2020-12-23 16:47

    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)
    
    0 讨论(0)
提交回复
热议问题