YEAR (Transact-SQL)YEAR (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回代表指定 date 中年份的整數。Returns an integer that represents the year of the specified date.

如需所有 Transact-SQLTransact-SQL 日期和時間資料類型與函數的概觀,請參閱日期和時間資料類型與函數 (Transact-SQL)For an overview of all Transact-SQLTransact-SQL date and time data types and functions, see Date and Time Data Types and Functions (Transact-SQL).

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

YEAR ( date )  

引數Arguments

datedate
這是可解析成 timedatesmalldatetimedatetimedatetime2datetimeoffset 值的運算式。Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. date 引數可以是運算式、資料行運算式、使用者定義變數或字串常值。The date argument can be an expression, column expression, user-defined variable or string literal.

傳回類型Return Types

intint

傳回值Return Value

YEAR 會傳回與 DATEPART (year, date) 相同的值。YEAR returns the same value as DATEPART (year, date).

如果 date 僅包含時間部分,傳回值就是 1900 (基底年份)。If date only contains a time part, the return value is 1900, the base year.

範例Examples

下列陳述式會傳回 2010The following statement returns 2010. 這是年份。This is the number of the year.

SELECT YEAR('2010-04-30T01:01:01.1234567-07:00');  

下列陳述式會傳回 1900, 1, 1The following statement returns 1900, 1, 1. date 的引數是數字 0The argument for date is the number 0. SQL ServerSQL Server 會將 0 解譯為 1900 年 1 月 1 日。interprets 0 as January 1, 1900.

SELECT YEAR(0), MONTH(0), DAY(0);  

範例:Azure SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse and 平行處理資料倉儲Parallel Data Warehouse

下列陳述式會傳回 1900, 1, 1The following statement returns 1900, 1, 1. date 的引數是數字 0The argument for date is the number 0. SQL ServerSQL Server 會將 0 解譯為 1900 年 1 月 1 日。interprets 0 as January 1, 1900.

SELECT TOP 1 YEAR(0), MONTH(0), DAY(0);  

另請參閱See Also

CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)