How to get a float result by dividing two integer values using T-SQL?

后端 未结 8 1825
暖寄归人
暖寄归人 2020-11-22 15:39

Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like:

select 1/3
         


        
8条回答
  •  借酒劲吻你
    2020-11-22 15:52

    I understand that CASTing to FLOAT is not allowed in MySQL and will raise an error when you attempt to CAST(1 AS float) as stated at MySQL dev.

    The workaround to this is a simple one. Just do

    (1 + 0.0)
    

    Then use ROUND to achieve a specific number of decimal places like

    ROUND((1+0.0)/(2+0.0), 3)
    

    The above SQL divides 1 by 2 and returns a float to 3 decimal places, as in it would be 0.500.

    One can CAST to the following types: binary, char, date, datetime, decimal, json, nchar, signed, time, and unsigned.

提交回复
热议问题