2

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

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
Edward
  • 91
  • 1
  • 2
  • 13

2 Answers2

9

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))
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
3

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

Jeff B
  • 535
  • 1
  • 6
  • 15