DATETIME2FROMPARTS (Transact-SQL)DATETIME2FROMPARTS (Transact-SQL)

適用於: 是SQL Server (從 2012 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2012) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

此函式會傳回指定日期和時間引數的 datetime2 值。This function returns a datetime2 value for the specified date and time arguments. 傳回值具有 precision 引數所指定的精確度。The returned value has a precision specified by the precision argument.

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

語法Syntax

DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision )  

引數Arguments

yearyear
指定年份的整數運算式。An integer expression that specifies a year.

monthmonth
指定月份的整數運算式。An integer expression that specifies a month.

dayday
指定日期的整數運算式。An integer expression that specifies a day.

hourhour
指定小時的整數運算式。An integer expression that specifies the hours.

minuteminute
指定分鐘的整數運算式。An integer expression that specifies the minutes.

secondsseconds
指定秒的整數運算式。An integer expression that specifies the seconds.

fractionsfractions
指定小數秒值的整數運算式。An integer expression that specifies a fractional seconds value.

有效位數precision
整數運算式,指定 DATETIME2FROMPARTS 會傳回之 datetime2 值的精確度。An integer expression that specifies the precision of the datetime2 value that DATETIME2FROMPARTS will return.

傳回類型Return types

datetime2( precision )datetime2( precision )

RemarksRemarks

DATETIME2FROMPARTS 會傳回完整初始化的 datetime2 值。DATETIME2FROMPARTS returns a fully initialized datetime2 value. 如果至少一個必要引數具有無效的值,DATETIME2FROMPARTS 會引發錯誤。DATETIME2FROMPARTS will raise an error if at least one required argument has an invalid value. 如果至少一個必要引數具有 Null 值,則 DATETIME2FROMPARTS 會傳回 Null。DATETIME2FROMPARTS returns null if at least one required argument has a null value. 不過,如果 precision 引數為 Null 值,DATETIME2FROMPARTS 會引發錯誤。However, if the precision argument has a null value, DATETIME2FROMPARTS will raise an error.

fractions 引數相依於 precision 引數。The fractions argument depends on the precision argument. 例如,precision 值為 7 的每個部分表示 100 奈秒;precision 為 3 的每個部分表示 1 毫秒。For example, for a precision value of 7, each fraction represents 100 nanoseconds; for a precision of 3, each fraction represents a millisecond. 如果 precision 值為零,fractions 也必須為零,否則 DATETIME2FROMPARTS 會引發錯誤。For a precision value of zero, the value of fractions must also be zero; otherwise, DATETIME2FROMPARTS will raise an error.

此函式支援遠端處理到 SQL Server 2017SQL Server 2017 伺服器及更新版本。This function supports remoting to SQL Server 2017SQL Server 2017 servers and above. 它不支援遠端處理到版本低於 SQL Server 2017SQL Server 2017 的伺服器。It will not support remoting to servers that have a version below SQL Server 2017SQL Server 2017.

範例Examples

A.A. 不包含秒之小數部分的範例An example without fractions of a second

SELECT DATETIME2FROMPARTS ( 2010, 12, 31, 23, 59, 59, 0, 0 ) AS Result;  

以下為結果集:Here is the result set.

Result  
---------------------------  
2010-12-31 23:59:59.0000000  
  
(1 row(s) affected)  

B.B. 含秒的小數部分的範例Example with fractions of a second

此範例示範 fractionsprecision 參數的用法:This example shows the use of the fractions and precision parameters:

  1. fractions 的值為 5、precision 的值為 1,則 fractions 的值表示 5/10 秒。When fractions has a value of 5, and precision has a value of 1, the value of fractions represents 5/10 of a second.

  2. fractions 的值為 50、precision 的值為 2,則 fractions 的值表示 50/100 秒。When fractions has a value of 50, and precision has a value of 2, the value of fractions represents 50/100 of a second.

  3. fractions 的值為 500、precision 的值為 3,則 fractions 的值表示 500/1000 秒。When fractions has a value of 500, and precision has a value of 3, then the value of fractions represents 500/1000 of a second.

SELECT DATETIME2FROMPARTS ( 2011, 8, 15, 14, 23, 44, 5, 1 );  
SELECT DATETIME2FROMPARTS ( 2011, 8, 15, 14, 23, 44, 50, 2 );  
SELECT DATETIME2FROMPARTS ( 2011, 8, 15, 14, 23, 44, 500, 3 );  
GO  

以下為結果集:Here is the result set.

----------------------  
2011-08-15 14:23:44.5  
  
(1 row(s) affected)  
  
----------------------  
2011-08-15 14:23:44.50  
  
(1 row(s) affected)  
  
----------------------  
2011-08-15 14:23:44.500  
  
(1 row(s) affected)  

另請參閱See also

datetime2 (Transact-SQL)datetime2 (Transact-SQL)