資料類型優先順序 (Transact-SQL)Data type precedence (Transact-SQL)

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

當一個運算子結合不同資料類型的運算式時,低優先順序資料類型會先轉換為高優先順序資料類型。When an operator combines expressions of different data types, the data type with the lower precedence is first converted to the data type with the higher precedence. 如果轉換不是支援的隱含轉換,就會傳回錯誤。If the conversion isn't a supported implicit conversion, an error is returned. 若運算子結合的運算元運算式具有相同資料類型,則作業結果就含有該資料類型。For an operator combining operand expressions having the same data type, the result of the operation has that data type.

SQL ServerSQL Server 會使用下列資料類型優先順序:uses the following precedence order for data types:

  1. 使用者自訂資料類型 (最高)user-defined data types (highest)
  2. sql_variantsql_variant
  3. xmlxml
  4. datetimeoffsetdatetimeoffset
  5. datetime2datetime2
  6. datetimedatetime
  7. smalldatetimesmalldatetime
  8. datedate
  9. timetime
  10. floatfloat
  11. realreal
  12. decimaldecimal
  13. moneymoney
  14. smallmoneysmallmoney
  15. bigintbigint
  16. intint
  17. smallintsmallint
  18. tinyinttinyint
  19. bitbit
  20. ntextntext
  21. texttext
  22. imageimage
  23. timestamptimestamp
  24. uniqueidentifieruniqueidentifier
  25. nvarchar (包含 nvarchar(max) )nvarchar (including nvarchar(max) )
  26. ncharnchar
  27. varchar (包含 varchar(max) )varchar (including varchar(max) )
  28. charchar
  29. varbinary (包含 varbinary(max) )varbinary (including varbinary(max) )
  30. binary (最低)binary (lowest)

另請參閱See also

資料類型 (Transact-SQL)Data Types (Transact-SQL)
運算式 (Transact-SQL)Expressions (Transact-SQL)
CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)