:::: MENU ::::

Data Type Precedence (T-SQL)

Data Modeling and Database Design, Data Types, dbDigger

Data Type Precedence (T-SQL)

When two expressions of different data types are combined by an operator, the data type precedence rules specify which data type is converted to the other. The data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type.

This is the precedence order for the Microsoft® SQL Server™ data types:

* datetime (highest)
* smalldatetime
* float
* real
* decimal
* money
* smallmoney
* int
* smallint
* tinyint
* bit
* ntext
* text
* image
* timestamp
* nvarchar
* nchar
* varchar
* char
* varbinary
* binary
* uniqueidentifier (lowest)

Data Type Precedence and Comparison Operators

These data type precedence rules are modified by the comparison operators. Comparison operators always return a Boolean data type. When a comparison operator is applied to expressions of two different data types, one of the expressions must be converted to the data type of the other before the comparison can be made. The data type precedence rules control this internal conversion, with the following exceptions:

* If one of the expressions is an aggregate function that is not in a subquery, and the other expression is not an aggregate, then the data type of the aggregate function is used regardless of the precedence rules.

* If one of the expressions is a column, and the other is not a column or aggregate function, the data type of the column is used regardless of the precedence rules.

Consult us to explore the Databases. Contact us