how to format getdate into YYYYMMDDHHmmSS

后端 未结 7 1501
春和景丽
春和景丽 2021-02-19 04:50

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?

I\'ve got the YYYYMMDD

7条回答
  •  北海茫月
    2021-02-19 05:34

    converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.

    select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
    select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
    

    Put them together like so inside the function

    DECLARE @result as varchar(20)
    set @result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
    print @result
    

    20131220 13:15:50

    As Thinhbk posted you can use select CONVERT(varchar,getdate(),20) or select CONVERT(varchar,getdate(),120) to get quite close to what you want.

提交回复
热议问题