Search code examples
sqlsql-servercastingdefault

avoid explicit cast to float?


Can

SELECT CAST(2.083 AS float) AS c

be written in a more compact way ?

Something like

SELECT 2.083f AS c

?


Solution

  • To avoid an explict CAST to float, specify a floating point constant expression:

    --these both return float with precision 53
    EXEC sp_describe_first_result_set N'SELECT CAST(2.083 AS float) AS c;';
    EXEC sp_describe_first_result_set N'SELECT 2.083E0 AS c;';
    
    --without scientific notation, the constant is interpreted as numeric(4,3)
    EXEC sp_describe_first_result_set N'SELECT 2.083 AS c;';