Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 308
tvgemert Avatar asked Jul 27 '11 08:07

tvgemert


People also ask

How do I get distinct years in SQL?

tableName::whereNotNull('columnName')->distinct()->get([DB::raw('YEAR(columnName) as year')]); This is select distinct year from a date column in laravel.

How do I get unique records in MySQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.

How do I select year from date in SQL?

The EXTRACT() function returns a number which represents the year of the date. The EXTRACT() function is a SQL standard function supported by MySQL, Oracle, PostgreSQL, and Firebird. If you use SQL Server, you can use the YEAR() or DATEPART() function to extract the year from a date.

How do I select a year from a timestamp in SQL?

Use the YEAR() function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time. This can be the name of a date/datetime/timestamp column or an expression returning one of those data types.


1 Answers

SELECT DISTINCT YEAR(created) FROM table 
like image 107
Dan Grossman Avatar answered Oct 14 '22 03:10

Dan Grossman