Playing with month names

  • T-SQL seems to have lots of great functions for playing around with dates but I can't seem to find one that would let me take an int and convert it to an abbreviated month name (ie. 1 = 'Jan').

    Since year and day don't matter, I came up with:

    SELECT

    ...

    DATENAME(mm, '2003-' + datafield + '-01') AS MonthName,

    ...

    which works but returns the full month name (ie. 'January', 'February' etc).

    The only other way I can think of is:

    SELECT

    ...

    CASE

    WHEN datafield = '1' THEN 'Jan'

    WHEN datafield = '2' THEN 'Feb'

    ...

    END

    which does what I want but seems very kludgy. Am I missing something vital here or is there just no simple way to turn '6' into 'Jun'?

    John

  • Well, there I go posting before thinking things through fully. Turns out that I could use:

    SELECT

    ...

    LEFT(DATENAME(mm, '2003-' + datafield + '-01'), 3) AS MonthName,

    ...

    to get what I want. Still seems a bit kludgy and I can't help but think that I'm missing a very obvious function that would do this for me but it's still better than 12 case statements.

    John

  • LEFT(DATEADD(m,DataField,-1),3)

    --Jonathan



    --Jonathan

  • Even better.

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

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