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?
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?
there are different ways of doing it
like
SELECT RIGHT('0' + RTRIM(MONTH('12-31-2012')), 2);
like
SELECT SUBSTRING(CONVERT(nvarchar(6),getdate(), 112),5,2)
see Fiddle
There may be other ways to get this.
Pinal Dave has a nice article with some examples on how to add trailing 0s to SQL numbers.
One way is using the RIGHT
function, which would make the statement something like the following:
SELECT RIGHT('00' + CAST(DATEPART(mm, @date) AS varchar(2)), 2)