DATETIMEOFFSETFROMPARTS (Transact-SQL)DATETIMEOFFSETFROMPARTS (Transact-SQL)

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

會傳回指定日期和時間引數的 datetimeoffset 值。Returns a datetimeoffset value for the specified date and time arguments. 傳回值的有效位數由 precision 引數指定,時差則由 offset 引數指定。The returned value has a precision specified by the precision argument, and an offset as specified by the offset arguments.

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

語法Syntax

DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, 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.

hour_offsethour_offset
指定時區時差之小時部分的整數運算式。An integer expression that specifies the hour portion of the time zone offset.

minute_offsetminute_offset
指定時區時差之分鐘部分的整數運算式。An integer expression that specifies the minute portion of the time zone offset.

有效位數precision
整數常值,指定 DATETIMEOFFSETFROMPARTS 所傳回 datetimeoffset 值的有效位數。An integer literal value that specifies the precision of the datetimeoffset value that DATETIMEOFFSETFROMPARTS will return.

傳回類型Return types

datetimeoffset( precision )datetimeoffset( precision )

RemarksRemarks

DATETIMEOFFSETFROMPARTS 會傳回完整初始化的 datetimeoffset 資料類型。DATETIMEOFFSETFROMPARTS returns a fully initialized datetimeoffset data type. offset 引數代表時區時差。The offset arguments represent the time zone offset. 針對省略的 offset 引數,DATETIMEOFFSETFROMPARTS 會假設時區時差為 00:00,也就是完全沒有時區時差。For omitted offset arguments, DATETIMEOFFSETFROMPARTS assumes a time zone offset of 00:00 - in other words, no time zone offset. 針對指定的 offset 引數,DATETIMEOFFSETFROMPARTS 預期兩個引數都有值,而且兩個值同時為正數值或負數值。For specified offset arguments, DATETIMEOFFSETFROMPARTS expects values for both arguments, and both values positive or negative. 如果 minute_offset 具有值而 hour_offset 沒有值,DATETIMEOFFSETFROMPARTS 會引發錯誤。If minute_offset has a value and hour_offset has no value, DATETIMEOFFSETFROMPARTS will raise an error. 如果其他引數的值無效,則 DATETIMEOFFSETFROMPARTS 會引發錯誤。DATETIMEOFFSETFROMPARTS will raise an error if the other arguments have invalid values. 如果至少一個必要引數具有 NULL 值,則 DATETIMEOFFSETFROMPARTS 會傳回 NULLIf at least one required arguments has a NULL value, then DATETIMEOFFSETFROMPARTS will return NULL. 不過,如果 precision 引數具有 NULL 值,則 DATETIMEOFFSETFROMPARTS 會引發錯誤。However, if the precision argument has a NULL value, then DATETIMEOFFSETFROMPARTS 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 也必須為零,否則 DATETIMEOFFSETFROMPARTS 會引發錯誤。For a precision value of zero, the value of fractions must also be zero; otherwise, DATETIMEOFFSETFROMPARTS 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 DATETIMEOFFSETFROMPARTS ( 2010, 12, 31, 14, 23, 23, 0, 12, 0, 7 ) AS Result;  

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

Result  
----------------------------------
2010-12-31 14:23:23.0000000 +12:00  
  
(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 DATETIMEOFFSETFROMPARTS ( 2011, 8, 15, 14, 30, 00, 5, 12, 30, 1 );  
SELECT DATETIMEOFFSETFROMPARTS ( 2011, 8, 15, 14, 30, 00, 50, 12, 30, 2 );  
SELECT DATETIMEOFFSETFROMPARTS ( 2011, 8, 15, 14, 30, 00, 500, 12, 30, 3 );  
GO  

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

----------------------------------  
2011-08-15 14:30:00.5 +12:30  
  
(1 row(s) affected)  
  
----------------------------------  
2011-08-15 14:30:00.50 +12:30  
  
(1 row(s) affected)  
  
----------------------------------  
2011-08-15 14:30:00.500 +12:30  
  
(1 row(s) affected)  

另請參閱See also

datetimeoffset (Transact-SQL)datetimeoffset (Transact-SQL)
AT TIME ZONE (Transact-SQL)AT TIME ZONE (Transact-SQL)