Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've got a really weird problem in (ofcourse) SQL.

I have a table(test):
ID     Total    Amount
1      10       3
2      10       2
3      10       1
4      10       0


Now I want to execute the following code:

SQL
SELECT *
FROM test
WHERE Total / Amount = 5


But if I run this query it will give a "Divide by zero error encountered" because of the last record where it will try to calculate "10 / 0". Of course this doesn't work, but there is no way to get it to work, tried this:

SQL
SELECT *
FROM 
(
   SELECT *
   FROM test
   WHERE Amount > 0
) a
WHERE a.Total / a.Amount = 5


But even that doesn't work.

Please help...
Posted
Updated 14-May-12 3:53am
v2

Try:
SQL
SELECT 
  *
FROM 
  test
WHERE 
  (Total / NULLIF(Amount, 0)) = 5
 
Share this answer
 
Comments
willempipi 14-May-12 10:08am    
Thanks for your solution.
Sandeep Mewara 14-May-12 10:28am    
Good to know.
This does work, WEIRD SQL SERVER!!!!

SQL
SELECT *
FROM test
WHERE Amount > 0
AND Total / Amount = 5
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900