3

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)))
Ruan
  • 3,969
  • 10
  • 60
  • 87

3 Answers3

4

I often use RIGHT as a cheap way to CONVERT.

select RIGHT(100+MONTH(CreatedDate),2)
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
2

With help from SQL Server Date Formats

You can try

DECLARE @Createdate DATETIME = '2008-07-25 13:43:48.000'
SELECT SUBSTRING(CONVERT(VARCHAR(8), @Createdate, 3), 4, 2)

And here is an example

SQL Fiddle DEMO

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
0

This is what you need

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

Replace(str(datepart(mm, CreatedDate), 2),' ','0')

Output: 07

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
  • uhm, are you sure? because i hover over MM and it says month in SQL server manager (and there is no mm, only MM) – Ruan Nov 01 '12 at 06:50