Generating a leading zero in DatePart(mm, a_date)

Hi all

SELECT CAST(DATEPART(yyyy, '03/04/2004') as char(4)) + CAST(DATEPART(mm, '03/04/2004') as char(2))
returns 20043

I am trying to edit this to return 200403, and it's not abundantly clear how to pull this off.  

Thanks.
-Jim
LVL 66
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAsked:
Who is Participating?
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT CAST(DATEPART(yyyy, '03/04/2004') as char(4)) + RIGHT('00'+CAST(DATEPART(mm, '03/04/2004') as varchar(2)),2)
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT CAST(DATEPART(yyyy, '03/04/2004') as char(4)) + RIGHT('00'+CAST(DATEPART(mm, '03/04/2004') as char(2)),2)
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
aneeshattingal - No love.  Still showed 20043 when run in SQL2K Query Analyzer.  -Jim
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
need to use varchar(2)  instead of char(2)
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
That worked.  Thank you.  -Jim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.