5

Why is ResultFloat = 0 in the query below?

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;   -- 1

Sql Server 2012

6

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 @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 |
+---------------------+------------------+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.