gotchasqlMinor
Why does implicit conversion from sql_variant (basetype decimal) not work well with float
Viewed 0 times
conversionwhyimplicitwithfloatdecimalsql_variantworkdoeswell
Problem
Why is ResultFloat = 0 in the query below?
Am I missing a point within the cast/convert, sql_variant docs?
Sql Server 2012
Am I missing a point within the cast/convert, sql_variant docs?
declare
@v sql_variant = convert(decimal(28,8), 20.0);
select sql_variant_property(@v, 'BaseType') as BaseType, -- 'decimal',
iif(convert(int, 10.0) < @v, 1, 0) as ResultInt, -- 1
iif(convert(decimal, 10.0) < @v, 1, 0) as ResultDecimal, -- 1
iif(convert(float, 10.0) < @v, 1, 0) as ResultFloat, -- 0 !
iif(convert(float, 10.0) < convert(float, @v), 1, 0) as ResultFloatFloat, -- 1
iif(convert(float, 10.0) < convert(decimal(28,8), @v), 1, 0) as ResultFloatDecimal; -- 1Sql Server 2012
Solution
From sql_variant (Transact-SQL)
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.
The base data type family for
Approximate numeric is higher up in the hierarchy chart than Exact numeric so when you compare two sql_variant values where one is Approximate numeric and the other is Exact numeric the Approximate numeric value will always be considered greater.
The full hierarchy order is
+---------------------+------------------+
| Data type family | Members |
+---------------------+------------------+
| sql_variant | sql_variant |
+---------------------+------------------+
| Date and time | datetime2 |
| | datetimeoffset |
| | datetime |
| | smalldatetime |
| | date |
| | time |
+---------------------+------------------+
| Approximate numeric | float |
| | real |
+---------------------+------------------+
| Exact numeric | decimal |
| | money |
| | smallmoney |
| | bigint |
| | int |
| | smallint |
| | tinyint |
| | bit |
+---------------------+------------------+
| Unicode | nvarchar |
| | nchar |
| | varchar |
| | char |
+---------------------+------------------+
| Binary | varbinary |
| | binary |
+---------------------+------------------+
| Uniqueidentifier | uniqueidentifier |
+---------------------+------------------+
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.
The base data type family for
@v is Exact numeric and the base data type family for convert(float, 10.0) is Approximate numeric. Approximate numeric is higher up in the hierarchy chart than Exact numeric so when you compare two sql_variant values where one is Approximate numeric and the other is Exact numeric the Approximate numeric value will always be considered greater.
The full hierarchy order is
+---------------------+------------------+
| Data type family | Members |
+---------------------+------------------+
| sql_variant | sql_variant |
+---------------------+------------------+
| Date and time | datetime2 |
| | datetimeoffset |
| | datetime |
| | smalldatetime |
| | date |
| | time |
+---------------------+------------------+
| Approximate numeric | float |
| | real |
+---------------------+------------------+
| Exact numeric | decimal |
| | money |
| | smallmoney |
| | bigint |
| | int |
| | smallint |
| | tinyint |
| | bit |
+---------------------+------------------+
| Unicode | nvarchar |
| | nchar |
| | varchar |
| | char |
+---------------------+------------------+
| Binary | varbinary |
| | binary |
+---------------------+------------------+
| Uniqueidentifier | uniqueidentifier |
+---------------------+------------------+
Context
StackExchange Database Administrators Q#56722, answer score: 7
Revisions (0)
No revisions yet.