Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have month and year in a varchar field and I want to retrieve Month Name from this field data.

this field month hold the data as 04-2020, 05-2020 etc.

I want to get April, May from these values.

I tried the query but it returns null.

Please help.

What I have tried:

SELECT MONTHNAME(date_format(str_to_date(I.month, '%m-%Y'), '%M, %Y')) as month from invoices I
Updated 21-Feb-21 3:39am

1 solution

Try this:
SELECT MONTHNAME(STR_TO_DATE("04-2020", "%m-%Y"));
SQL Fiddle[^]

You can test with MariaDb here: MariaDB 10.4 | db<>fiddle[^]
Share this answer
nyt1972 22-Feb-21 1:34am    
It say Warning: #1411 Incorrect datetime value: '04-2020' for function str_to_date and return NULL value too.

My MySQL version shows:

mysql Ver 15.1 Distrib 10.4.14-MariaDB
RickZeeland 22-Feb-21 2:58am    
Maybe add "01-" at the begin of "04-2020" ?
nyt1972 22-Feb-21 3:01am    
sorry I type %M instead of %m
RickZeeland 22-Feb-21 3:01am    
To err is human :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900