- Instant help with your Sql coding problems

Select year from a Unix timestamp in MySQL

Question:
How to select year from a Unix timestamp in MySQL?
Answer:
SELECT FROM_UNIXTIME(event_time, '%Y') AS year FROM events;
Description:

If a MySQL database table has a Unix timestamp stored in an INT field, it must first be converted to an easy-to-read format when generating a report. 

This conversion can be done with the FROM_UNIXTIME() function, which returns a representation of unix_timestamp as a DateTime or character string value. 

If you want to refine this further, for example, if you only need the year value, you can use the '%Y ' format string as a second parameter.

Share "How to select year from a Unix timestamp in MySQL?"