Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone tell me how to convert month name to month number in SQL?

1 Answer

0 votes
by (119k points)

You can use DATEPART() function to extract the month number from the month name in date:

SELECT DATEPART (MM, 'January 01 2020') - returns 1

SELECT DATEPART (MM, ‘April 01 2020') - returns 4

SELECT DATEPART (MM, 'May 01 2020')  - returns 5

If your column has the only month and you want to convert into month number then there is no inbuilt function for that. You can use the following SQL query to convert month name to month:

CASE WHEN MonthName= 'January' THEN 1

     WHEN MonthName = 'February' THEN 2

     ...

     WHEN MonthName = 'December' TNEN 12

END AS MonthNumber

If you are interested in SQL, then take up this SQL Training course by Intellipaat that offers instructor-led training, certification, and job assistance. 

You can learn in-depth about SQL statements, queries and become proficient in SQL queries by enrolling in our industry-recognized SQL training.

Related questions

0 votes
1 answer
asked May 6, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Apr 22, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer

Browse Categories

...