SQL change format of Datepart Month

Ruan picture Ruan · Nov 1, 2012 · Viewed 7.6k times · Source

How do you change the DATEPART format of the month to include the 0 in front of the month?

For instance 5 should be 05.

What I have tried, but none of it works, (only gives me 5)

Createdate = 2008-07-25 13:43:48.000

CONVERT(varchar(2), DATEPART(MONTH,(CreatedDate)))
CONVERT(varchar(2), DATEPART(MM,(CreatedDate)))

Answer

RichardTheKiwi picture RichardTheKiwi · Nov 1, 2012

I often use RIGHT as a cheap way to CONVERT.

select RIGHT(100+MONTH(CreatedDate),2)