Split on SQL Server

In SQL Server 2005 Express, the result is lower

SELECT 100/15 - Result 6

But I want to get an approximate value of 7 (for example, using a calculator)

100/15 = 6.6666 ..

How to do it on SQL Server?

+4
source share
4 answers

You should use decimal numbers, for example. 100 / 15.0. If you use integers, the result is interpreted as an integer and truncated to the largest integer that is less than or equal to the result.

+10
source

SELECT cast (100 as float) / (15 as float)

+5
source

You want SQL Server to perform floating point division rather than integer division. If you use literal values, as in your example, the denominator suffix is ​​with .0 to force SQL Server to treat it as a floating point value.

 SELECT 100 / 15.0 

Otherwise, make sure you specify your variables as FLOAT .

+3
source

Try declaring the variables as float:

 DECLARE @var1 FLOAT DECLARE @var2 FLOAT SET @var1 = 100 SET @var2 = 15 SELECT @var1 / @var2 
+2
source

Source: https://habr.com/ru/post/1341759/


All Articles