Dileep Kumar
Dileep Kumar

Reputation: 510

Convert month Fullname to month number

Is There any built-in function in MySQL for getting Month Number for given Month Full

Name like 'January','March',etc.

I tried with MONTH() function to get month number from the date like this:

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

but it is not working for Fullname like April

Upvotes: 1

Views: 108

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18598

Try with DATE_FORMAT option

SELECT MONTH(STR_TO_DATE('April','%M')) as Month;

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80649

For matching full month names, you'll have to use the %M specifier in DATE_FORMAT:

%M      Month name (January..December)

Therefore, the following would give this:

SELECT MONTH(STR_TO_DATE('April','%M')) /* result is 4

Upvotes: 2

Related Questions