Date Formatting

  • I am using DatePart to strip the pieces out of a date field to create mm/dd/yyyy format.  This is simple, however, it will only do m/d/yyyy format. 

    Any ideas how to force to mm/dd/yyyy even if it is a single digit day or month?  I can do this with a long CASE clause, but I know there has got to be a better way.

     

    Thank you!

  • is this what you want ?!

    "select convert(varchar, dateField, 101) as formattedDate from myTable"







    **ASCII stupid question, get a stupid ANSI !!!**

  • YES, exactly!  Thank you!

  • Just in case you want a general soulution to the padding problem you can try this if you ever have a need.

    declare @month as varchar(10)

    set @month = datepart(month, '01/01/2005')

    select right('0' + @month,2)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply