sql_variant (Transact-SQL)

sql_variant (Transact-SQL)

 

Updated: October 2, 2015

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)noAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

A data type that stores values of various SQL Server-supported data types.

Applies to: SQL Server (SQL Server 2008 through current version), Azure SQL Database.

Topic link icon Transact-SQL Syntax Conventions

  
sql_variant  

sql_variant can be used in columns, parameters, variables, and the return values of user-defined functions. sql_variant enables these database objects to support values of other data types.

A column of type sql_variant may contain rows of different data types. For example, a column defined as sql_variant can store int, binary, and char values.

sql_variant can have a maximum length of 8016 bytes. This includes both the base type information and the base type value. The maximum length of the actual base type value is 8,000 bytes.

A sql_variant data type must first be cast to its base data type value before participating in operations such as addition and subtraction.

sql_variant can be assigned a default value. This data type can also have NULL as its underlying value, but the NULL values will not have an associated base type. Also, sql_variant cannot have another sql_variant as its base type.

A unique, primary, or foreign key may include columns of type sql_variant, but the total length of the data values that make up the key of a specific row should not be more than the maximum length of an index. This is 900 bytes.

A table can have any number of sql_variant columns.

sql_variant cannot be used in CONTAINSTABLE and FREETEXTTABLE.

ODBC does not fully support sql_variant. Therefore, queries of sql_variant columns are returned as binary data when you use Microsoft OLE DB Provider for ODBC (MSDASQL). For example, a sql_variant column that contains the character string data 'PS2091' is returned as 0x505332303931.

The sql_variant data type belongs to the top of the data type hierarchy list for conversion. For sql_variant comparisons, the SQL Server data type hierarchy order is grouped into data type families.

Data type hierarchyData type family
sql_variantsql_variant
datetime2Date and time
datetimeoffsetDate and time
datetimeDate and time
smalldatetimeDate and time
dateDate and time
timeDate and time
floatApproximate numeric
realApproximate numeric
decimalExact numeric
moneyExact numeric
smallmoneyExact numeric
bigintExact numeric
intExact numeric
smallintExact numeric
tinyintExact numeric
bitExact numeric
nvarcharUnicode
ncharUnicode
varcharUnicode
charUnicode
varbinaryBinary
binaryBinary
uniqueidentifierUniqueidentifier

The following rules apply to sql_variant comparisons:

  • When sql_variant values of different base data types are compared and the base data types are in different data type families, the value whose data type family is higher in the hierarchy chart is considered the greater of the two values.

  • When sql_variant values of different base data types are compared and the base data types are in the same data type family, the value whose base data type is lower in the hierarchy chart is implicitly converted to the other data type and the comparison is then made.

  • When sql_variant values of the char, varchar, nchar, or nvarchar data types are compared, their collations are first compared based on the following criteria: LCID, LCID version, comparison flags, and sort ID. Each of these criteria are compared as integer values, and in the order listed. If all of these criteria are equal, then the actual string values are compared according to the collation.

When handling the sql_variant data type, SQL Server supports implicit conversions of objects with other data types to the sql_variant type. However, SQL Server does not support implicit conversions from sql_variant data to an object with another data type.

The following table lists the types of values that cannot be stored by using sql_variant:

varchar(max)varbinary(max)
nvarchar(max)xml
textntext
imagerowversion (timestamp)
sql_variantgeography
hierarchyidgeometry
User-defined typesdatetimeoffset

CAST and CONVERT (Transact-SQL)
SQL_VARIANT_PROPERTY (Transact-SQL)

Community Additions

ADD
Show:
© 2016 Microsoft