SQL_VARIANT_PROPERTY#
Returns the base type and other information about a sql_variant value.
Syntax#
SQL_VARIANT_PROPERTY ( expression , property )
Arguments#
expression
Is an expression of any type a sql_variant can hold. An expression of a type other than sql_variant is
described as the variant it would become, with its own type as the base type. An expression of a type a variant cannot
hold - varchar(max), nvarchar(max), varbinary(max), xml, text, ntext, image,
rowversion - raises error 206 Operand type clash: varchar(max) is incompatible with sql_variant when the
statement is compiled, as in SQL Server.
property
Is the name of the property to return, a varchar(128). The name is not case-sensitive.
Property |
Description |
Returned type |
|---|---|---|
BaseType |
The name of the base type of the value, for example |
sysname |
Precision |
The precision of a numeric base type, or the number of characters of a date and time type. |
int |
Scale |
The scale of a numeric or a date and time base type. |
int |
TotalBytes |
The number of bytes the value takes inside the variant, its type information included. |
int |
Collation |
The collation of a character base type; NULL for any other base type. |
sysname |
MaxLength |
The maximum length, in bytes, of the base type. |
int |
Return types#
sql_variant. A NULL expression, and a property that is not one of the names above, return NULL.
Remarks#
A sql_variant value keeps the type it was converted from - its base type - wherever it travels: in a
variable, in a table column, through a stored procedure parameter and to the client. The base type decides how
the value converts: a date refuses a conversion to int with error 529 as in SQL Server, money
converts to text with two decimals, and a char(n) keeps its trailing spaces. DATALENGTH of a variant
answers the length of the value in its base type.
A sql_variant value read from a SQL Server source keeps the base type, scale and collation it has there, so a
date, a smallmoney or a char(10) in Polish_CS_AS stays one after it reaches a variable or a temporary
table. A value read through DISTINCT, GROUP BY, a set operation such as UNION or the text of an
OPENQUERY is given the base type the client can tell, as a value of any other data source is: the base type its
value stands for - a whole number an integer type, a date and time datetime2, text nvarchar.
SQL_VARIANT_PROPERTY over a column of a SQL Server source runs on that source and answers the source’s own base
type.
Example#
DECLARE @v sql_variant = CAST(1234.5678 AS money);
SELECT SQL_VARIANT_PROPERTY(@v, 'BaseType') AS BaseType, -- money
SQL_VARIANT_PROPERTY(@v, 'Precision') AS Precision, -- 19
SQL_VARIANT_PROPERTY(@v, 'Scale') AS Scale, -- 4
CAST(@v AS varchar(30)) AS Text; -- 1234.57