Datepart Month question

  • How do I receive the month in 2 digits in select (mm,getdate())? Every month earlier then October is returned as 1 digit. Funny thing, when I execute

    select

      right(datepart(mm,getdate()),2)

    I receive '07'...

    I can construct some SQL to accomplish this, but I would think it would be easy to format the output.

    Greetz,
    Hans Brouwer

  • Not that I know of so I would just use this:

    select

    'Month' =

    case len(datepart(mm,getdate()))

    when 1 then

    '0' + cast(datepart(mm,getdate()) as varchar)

    else

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

    end

  • Tnx for answering, Aiden. I had something similar, but was hoping for a more elegant solution. I can't find it, so I guess I'll have to use the brute-force approach....

    Greetz,
    Hans Brouwer

  • SELECT REPLACE(STR(MONTH(GETDATE()), 2), ' ', '0')

  • select MM = right(100+month(getdate()),2)
    MM   
    ---- 
    03
    (1 row(s) affected)
     
  • Tnx for the answers, really helped me.

    Greetz,
    Hans Brouwer

Viewing 6 posts - 1 through 6 (of 6 total)

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