Skip to content
Advertisement

Get 2 Digit Number For The Month

I have an integer column “Month” I would like to get 2 digit number for month.

This is what I have tried: DATEPART(mm, @Date)

It returns one digit for months January to September I am using SQL Server 2008

Anyone has suggestion?

Advertisement

Answer

there are different ways of doing it

  • Using RTRIM and specifing the range:

like

SELECT RIGHT('0' + RTRIM(MONTH('12-31-2012')), 2); 
  • Using Substring to just extract the month part after converting the date into text

like

SELECT SUBSTRING(CONVERT(nvarchar(6),getdate(), 112),5,2)

see Fiddle

There may be other ways to get this.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement