YEAR
returns the year from the specified date.
This function is equivalent to DATEPART(year, date).
This example returns the year part from the specified date.
SELECT YEAR('12-18-2022 11:54:07') AS Year
Year |
---|
2022 |
A common use is to get the current year.
SELECT YEAR(GETDATE()) as 'Current Year'
Current Year |
---|
2023 |
Syntax of the YEAR function .
YEAR(date)
date
-- a date or datetime value.
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT FirstName, LastName, OrderNumber,
OrderDate, TotalAmount
FROM [Order] O
JOIN Customer C ON C.Id = O.CustomerId
WHERE YEAR(OrderDate) = 2013
FirstName | LastName | OrderNumber | OrderDate | TotalAmount |
---|---|---|---|---|
Marias | Anders | 542773 | 2013-08-25 00:00:00.000 | 1086.00 |
Marias | Anders | 542822 | 2013-10-03 00:00:00.000 | 878.00 |
Marias | Anders | 542832 | 2013-10-13 00:00:00.000 | 330.00 |
Ana | Trujillo | 542889 | 2013-11-28 00:00:00.000 | 320.00 |
Ana | Trujillo | 542755 | 2013-08-08 00:00:00.000 | 479.75 |
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
SELECT YEAR(OrderDate) AS Year,
COUNT(Id) AS Count,
SUM(TotalAmount) AS 'Total Sales'
FROM [Order]
GROUP BY YEAR(OrderDate)
ORDER BY YEAR(OrderDate) DESC
Year | Count | Total Sales |
---|---|---|
2014 | 270 | 469771.34 |
2013 | 408 | 658388.75 |
2012 | 152 | 226298.50 |