Home » Mysql » Get all unique years from a date column using SQL (MySQL)

Get all unique years from a date column using SQL (MySQL)

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a MySQL table with a datetime column named ‘created’ which contains the date and time the record was inserted. This table has about 30 records right now. I want to get all occuring years (unique, not per record) from this table. Is there a way to do this using a MySQL query?

Answers:
SELECT DISTINCT YEAR(created) FROM table

Questions:
Answers:

Something on these lines should work (Warning: untested code ahead)

select year(created) as year, count(*) as count from table_name group by year

This will also give number of records for each year.

Questions:
Answers:
SELECT EXTRACT(year from created) as year
FROM your_table
GROUP BY year

should work 🙂

Questions:
Answers:

SELECT DISTINCT YEAR(Date) AS MYR
FROM YOUR_DBF_HERE
ORDER BY MYR

Questions:
Answers:
Select 
EXTRACT(year from your_column_name) as MYEAR from your_table_name
group by EXTRACT(year from your_column_name)

The above worked for me…