Convert Date into a format to extract the Year in MySQL Select Statement

I have a date in a table formatted like this:

31-May-90

I need to get the year – 1990 in my MySQL statement. I tried:

year(tblPhotoManage.Date) AS cpYear

Obviously this won’t work. How can I get the year from this date format?

>Solution :

First convert the string that looks a bit like a date into a MySQL DATE datatype, then using the YEAR() function that only works on DATE/DATETIME datatypes to get the year

SELECT YEAR(STR_TO_DATE('31-May-90','%d-%M-%y'));

Leave a Reply