Edward
Edward

Reputation: 91

Division of 2 numbers using CAST function in SQL server 2008R2

I have two numbers I want to divide: 5262167 / 162333331 When verified using windows calculator (calc.exe) the result is 0.0324158136076195 but when used simple select with CAST function in SQL Server 2008R2 I don't have same result. Here is what I'm running in SQL editor:

select CAST((5262167 / 162333331) as decimal(18,8))

and the result is 0.00000000

Upvotes: 2

Views: 14399

Answers (2)

Jeff B
Jeff B

Reputation: 545

Another less elegant way is: select ( 5262167 * 1.0 ) / ( 1623333331 * 1.0 ) ). I usually forget to cast them and just multiply by 1.0.

Upvotes: 3

Esoteric Screen Name
Esoteric Screen Name

Reputation: 6112

You're doing integer division, which will truncate any remainders. 5262167 < 162333331, so your result is 0. Cast your input before dividing.

select CAST(5262167 as decimal(18,8)) / CAST(162333331 as decimal(18,8))

Upvotes: 9

Related Questions