SQL YEAR() Function
The SQL YEAR() is a function, and returns or extract year portion(a four digit number) of a given input date value.
The SQL YEAR() function is supports only date or datetime based table columns or fields.
It can be used in SELECT statement as well in where clause.
Related Links
SQL YEAR() Syntax
The below syntax is used to get year value from a given input date value using YEAR() function.
For SQL SERVER / MY SQL / MS ACCESS
SELECT YEAR('valid_date_value');
The below syntax is used to read year portion value from a given table column using YEAR() function
For MS ACCESS / SQL SERVER / MY SQL
SELECT YEAR(date_column_name1) FROM table_name;
SQL YEAR() Example - Using Expression Or Formula
The following SQL SELECT statement will extract the year value from a given input date value. We use now() function to get the input date value.
SELECT
NOW() AS 'Current Date and Time',
YEAR(NOW()) AS 'Year Value';
The result of above query is:
Current Date and Time | Year Value |
---|---|
24-08-2012 19:37:30 | 2012 |
Sample Database Table - BookOrder
BookID | BookName | OrderDate | Qty | DeliveryDate |
---|---|---|---|---|
1 | Foundations Of Sql Server 2008 | 25-08-1999 14:03:22 | 13 | 28-09-1999 09:01:39 |
2 | My SQL Complete Reference | 19-09-2007 20:26:04 | 3 | 23-09-2007 14:05:09 |
3 | Simply Oracle | 22-03-2009 16:02:06 | 8 | 27-03-2009 17:37:52 |
4 | Postgresql Official Documentation | 14-06-2005 18:37:10 | 1 | 17-06-2005 15:53:26 |
SQL YEAR() Example - With Table Column
The following SQL SELECT statement display the column "OrderDate" and "DeliveryDate" from the "BookOrder" table. The YEAR() function will extract the year value(a four digit number) from the column "OrderDate" and we will stored that value to a new column called "Order Year" and "Delivery Year".
SELECT
OrderDate, YEAR(OrderDate) As 'Order Year',
DeliveryDate, YEAR(DeliveryDate) As 'Delivery Year'
FROM BookOrder;
The result of above query is:
OrderDate | Order Year | DeliveryDate | Delivery Year |
---|---|---|---|
25-08-1999 14:03:22 | 1999 | 28-09-1999 09:01:39 | 1999 |
19-09-2007 20:26:04 | 2007 | 23-09-2007 14:05:09 | 2007 |
22-03-2009 16:02:06 | 2009 | 27-03-2009 17:37:52 | 2009 |
14-06-2005 18:37:10 | 2005 | 17-06-2005 15:53:26 | 2005 |
Related Links