-1

I have this statement:

Select (30 - 5) * 700 / 30 as A , 700 - (5 * 700 / 30) as B

which has two ways to calculate the same equation.

These two equations should result in 583.33 if you made them by your calculator but the previous statement results in 583 for field A, and 584 for field B.

Both are wrong and both are integer not decimal.

I want to know what's the right way to write this statement so I can get 583.33.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You need to cast it in select statement first see here. [Cast select result](http://stackoverflow.com/questions/9259254/select-19-12-return-1-i-need-in-decimal-that-is-1-58-sqlserver-2005) –  Dec 11 '16 at 08:11

2 Answers2

1

Your expression is implicitly using an INT datatype. Try it this way to allow it to use a DECIMAL datatype

Select (30.000 - 5.000) * 700.000 / 30.000 as A ,  
       700.000 - (5.000 * 700.000 / 30.000) as B
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
0

Use Cast function:

As next:

   Select Cast((30 - 5) * 700 as DECIMAL(19,2) ) / 30 as A , 
          700 - ( Cast (5 * 700 as decimal (19,2 )) / 30) as B

Result:

A              B
583.333333  583.333334
ahmed abdelqader
  • 3,409
  • 17
  • 36