Convert Date to a string

  • How do I pull the month out of date, and convert it to a string value?

    In other words, if the month is January, I want to pull out 01. February, I want 02 (but the output needs to be a string)

  • select case when datepart(mm,getdate())<=9 then '0'+cast(datepart(mm,getdate())as varchar)

    else cast(datepart(mm,getdate())as varchar) end

  • OR:

    select  right('00'+ cast(month(getdate()) as varchar(2)),2)


    * Noel

  • Another one:

    SELECT REPLACE(STR(DATEPART(mm,GETDATE()),2), ' ', '0')

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • And, a totally different idea...

    Create a table that has all the date conversions done, a dimension table.  Then simply link to the table and pull only the month converted to text.  By using a table, you essentailly never have to do any date conversions like this again.

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

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