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

## 語法Syntax

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

## 引數Arguments

yearyear

monthmonth

dayday

hourhour

minuteminute

secondsseconds

fractionsfractions

## 傳回類型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.

## 範例Examples

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

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

``````Result
---------------------------
2010-12-31 23:59:59.0000000

(1 row(s) affected)
``````

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

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
``````

``````----------------------
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)
``````