How do I get the month and day with leading 0's in SQL? (e.g. 9 => 09)

前端 未结 11 1143
旧巷少年郎
旧巷少年郎 2020-12-08 06:03
DECLARE @day CHAR(2)

SET @day = DATEPART(DAY, GETDATE())

PRINT @day

If today was the 9th of December, the above would print \"9\".

I want

11条回答
  •  孤城傲影
    2020-12-08 06:50

    SQL Server 2012+ (for both month and day):

    SELECT FORMAT(GetDate(),'MMdd')
    

    If you decide you want the year too, use:

    SELECT FORMAT(GetDate(),'yyyyMMdd')
    

提交回复
热议问题