0 votes
1 view
in SQL by (36k points)

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

1 Answer

0 votes
by (86.8k 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. 

Related questions

0 votes
1 answer
asked May 6 in SQL by Sudhir_1997 (36k points)
0 votes
1 answer
asked Apr 22 in SQL by Sudhir_1997 (36k points)
0 votes
1 answer
asked Jul 22, 2019 in SQL by Tech4ever (20.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...