Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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 |
+---------------------+------------------+
share|improve this answer
1  
I had always interpreted that statement to mean an implicit cast occurred to the higher family (somehow) but its truly literal! Thanks. –  crokusek Jan 14 at 17:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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