Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading