Divide in SQL Server
Asked Answered
O

5

7

In SQL Server 2005 Express, the result is below

SELECT 100 / 15 --Result 6

But I wanna get approximate value 7 (like using calculator)

100 / 15 = 6.6666..

How to make it in SQL Server?

Orthoptic answered 1/3, 2011 at 5:55 Comment(1)
Now I got it. Thanks all :). SELECT ROUND(100 / 15.00,0)Orthoptic
B
15

You have to use decimal numbers e.g. 100 / 15.0. If you use integers the result is interpreted as an integer and truncated to the largest integer that is smaller than or equal to the result.

Beatnik answered 1/3, 2011 at 5:59 Comment(0)
A
8

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

Agosto answered 11/5, 2013 at 15:12 Comment(0)
P
5

You want SQL Server to perform floating-point divison, as opposed to integer division. If you're using literal values, as in your example, suffix the denominator with .0 to force SQL Server to treat it as a floating-point value.

SELECT 100 / 15.0

Otherwise, make sure you declare your variables as FLOAT.

Publus answered 1/3, 2011 at 5:59 Comment(0)
P
2

Try declaring variables as floats:

DECLARE @var1 FLOAT
DECLARE @var2 FLOAT
SET @var1 = 100
SET @var2 = 15
SELECT @var1 / @var2
Pedersen answered 1/3, 2011 at 5:59 Comment(0)
E
0

You need to multiply it with 0.1 and than multiply 10 to the whole result for getting the exact result. Example: (100*0.1/15)*10 will give you 6.666660 as result.

Estrada answered 15/7 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.