Hoow to I convert month name to month number?

Posted on 2007-10-03
Last Modified: 2008-01-09
This is kinda of silly but it's late.  I am getting the month name in my query using

But I need to use this field the order by clause so I need it to sort by month number 01 -12 , not alphabetically.  How do I convert month name back to number so order by works?

Question by:svasilakos
    LVL 142

    Accepted Solution

    you need to create a function yourself to "translate" the month name into the numerical value.
    I usually have a table with the translation values, so I can join/lookup in there
    LVL 14

    Expert Comment

    if t.transactiondate a database field
    why dont you direcrly order by your transaction date ?

    Author Comment

    Oh.. good question. Becuase I am runing a larger query and converting to month name and than storing this in a #temp table that I actually run my report off of.

    I think I'll do something similar to what angelIII said... I wasn't sure if there was a easy convert statement I was missing..

    LVL 9

    Expert Comment

    by:Tone' Shelby
    I have used something like what was mentioned above but found that if I added datepart(month,fieldname) in addtion to DATENAME(month,fieldname) to the group by expression it works like a snap foe example to find the Number of Hires for a given month for each year .....

    as [Month],
    datepart(year,[HireDate]) as [Year],
    count(*) as [NumberHired]
    from [AdventureWorks].[HumanResources].[Employee]
    group by

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now