time format in SQL Server

前端 未结 7 1733
心在旅途
心在旅途 2020-12-04 01:20

Does anyone know how can I format a select statement datetime value to only display time in SQL Server?

example:

Table cuatomer
id   name   datetime
         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 02:04

    This will get the time from a datetime value and also give the am or pm add on

    SELECT RIGHT('0'+LTRIM(RIGHT(CONVERT(varchar,getDate(),100),8)),7) 
    

    will always return the date in HH:mmAM format.

    Note the lack of space

    Or

    SELECT REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(varchar,getDate(),100),7)),7),'AM',' AM'),'PM',' PM')
    

    will always return the date in HH:mm AM format.

    Hope that helps.

    PK

提交回复
热议问题