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

How to extract year from a DATE column and insert into new column in MySQL

I have a column "date" with data type date and format "YYYY-MM-DD".

I would like to create a new column having only the year "YYYY".

I tried YEAR() and EXTRACT() functions but to my understanding those are queries and I cannot insert them into a column later on.

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

Any thoughts on that?(please keep in mind that I am novice at best)

>Solution :

First you need to update your table’s schema with an ALTER TABLE statement as follows:

ALTER TABLE <your_tab_name> ADD COLUMN `year` INT;

Then you can use an UPDATE statement and your function YEAR to update your newly created field with the extracted year value.

UPDATE <your_tab_name> 
SET `year` = YEAR(`date`)
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