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

后端 未结 8 1781
暖寄归人
暖寄归人 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 16:05

    Because SQL Server performs integer division. Try this:

    select 1 * 1.0 / 3
    

    This is helpful when you pass integers as params.

    select x * 1.0 / y
    
    0 讨论(0)
  • 2020-11-22 16:10

    Use this

    select cast((1*1.00)/3 AS DECIMAL(16,2)) as Result

    Here in this sql first convert to float or multiply by 1.00 .Which output will be a float number.Here i consider 2 decimal places. You can choose what you need.

    0 讨论(0)
提交回复
热议问题