Joseph Erickson Joseph Erickson - 7 months ago 19
SQL Question

MYSQL reverse engineering EXTRACT()

I had a date field like

2014-12-30
and I did something like
EXTRACT(YEAR_MONTH FROM date) as yr_month
which returned a value like
201412
.

I'd like to retrieve the month from this new value, reverse engineer it back to the month. I tried
MONTH(yr_mnth)
and also
MONTH(RIGHT(yr_mnth, 2))
.

Any thoughts on how to retrieve the month from this field?

Answer

Try the following:

SELECT MONTHNAME(STR_TO_DATE(RIGHT(yr_mnth, 2), '%m'));

This will get you the monthname from the month number. As you can see here: MySQL MONTHNAME() from numbers

Comments