Datename Help Please

  • I don't want to use case statement but want to convert numeric field to month name. Ex:

    Month       Month_Name

    1             January

    2             Feburary

    3             March

    and etc... Thanks for your help.

    Minh

  • I figured out a stupid way to do it.  But if someone shows me a better way, I will be really appreciate.

    My way is:

    select datename(mm,convert(datetime,[month]+'/1/00'))

    from mytable

  • Here's another stupid way:

    SELECT DATENAME(month, @month * 28)

    -- where @month contains an integer between 1 and 12.

    Can't see any more obvious way. It would perhaps be more elegant to write a UDF.

    Phil

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • How often are you going to need to do this conversion? If it's not often, do the conversions suggested. If you are going to do it often, there's the possibility you will need it in different scripts. In that case I suggest creating a table to hold the information.

    CREATE TABLE myMonths

      AS

      (MonthInt  INT,

       MonthName VARCHAR(9))

    Once you insert the values, you can do an table join and select the MonthName where the MonthInt equals whatever you want.

    -SQLBill

  • For what its worth...

    SQLBill's solution would allow for situations where you don't have 12 months in a year (e.g. in accounting scenarios you may have 13). I would go with that!

     

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

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