Link to home
Start Free TrialLog in
Avatar of Jim Horn
Jim HornFlag for United States of America

asked on

Month(GetDate()) to return '04' instead of 4

Hi all

I have the following SP line in progress...

declare @time_period_id_current int
set @time_period_id_current = CAST(CAST(YEAR(GETDATE()) as char(4))+ '' + CAST(MONTH(GETDATE()) as char(4))as int)

Where I'm trying to convert the current year and month into and integer with YYYYMM format, i.e. 200604.  The above line returns 20064.

How to change the above line to retain the leading '0' in the month?

Thanks in advance.
-Jim
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right('00' + CAST(MONTH(GETDATE()) as char(4))as int) ,2)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Horn

ASKER

Thanks guys.  -Jim
Glad to be of assistance. May all your days get brighter and brighter.