Datepart(dd, datefield) showing 1 digit for day when the day is less than 10

Hello experts:

Here's the sql:
select Lastbuild, (cast(datepart(YY,LastBuild) as VARCHAR(4))
  + '-'
  + cast(datepart(MM,LastBuild) as varchar(2))
  + '-'
  + cast(datepart(DD,LastBuild) as varchar(2)))
  from #tmpResults

Here's my input and the results:  

             lastbuild                          no col name
2011-11-29 19:59:21.410         2011-11-29
2011-12-06 08:08:42.157        2011-12-6

Who is Participating?
Why not just use convert?

SELECT CONVERT(varchar(10), LastBuild, 120)
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.