Search code examples
mysqlsqlstring-to-datetime

Convert month shortname to month number


I am searching for a built-in function in MySQL for getting Month Number for given Month Short Name like 'Jan','jul',etc.


Solution

  • Use STR_TO_DATE() function to convert String to Date like this:

    SELECT STR_TO_DATE('Apr','%b')
    

    And use MONTH() to get month number from the date like this:

    SELECT MONTH(STR_TO_DATE('Apr','%b'))
    

    See this SQLFiddle