SQLTeam.com | Weblogs | Forums

SQL code to pad leading zeros and get last 3 months varchar


#1

I need SQL code to pad leading zeros and get last 3 months as varchar. Sample code below. My syntax is not right.

Select RIGHT('00'+CONVERT(NVARCHAR(2),datepart(mm,dateadd(mm,-3,getdate())),2)

result I am after: 02


#2

Why 02 is it supose to be November , 11 ?

Anyway you missed one " ) " at the back

SELECT RIGHT ( '00' + CONVERT( NVARCHAR(2), DATEPART(MONTH, DATEADD ( MONTH, -3, GETDATE() ) ) ) , 2 )


#3

You can cheat like hell and make it much simpler...

SELECT CONVERT(NCHAR(2),DATEADD(mm,-3,GETDATE()),10);