Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI all i had a question on passing a parametr to a query, i am trying to fileter all the data which has more than 1000 bps i had my query like the below

SQL
select * from tbla 
where id=128
and good='G'
and abs(((impt*rate)/(v1*shares*rate))*10000)>1000

when i write a query like this i am getting result showing the rows which has more than 1000 bps

but when i write a query like this

SQL
declare @id as int 
set @id=128
select * from tbla
where id=@id
and good="G";
and abs(((impt*rate)/(v1*shares*rate))*10000)>1000

the query is booming out saying divide by zero error i dont understand whats the diffrence between both queries accept the parameter part i was trying to google it but no use can some one help me out with this.
Posted

1 solution

You can handle it using NULLIF

This is where NULLIF comes to the rescue. In the query below, the divisor will be changed to NULL if it was 0, setting the result of the entire calculation to NULL instead of causing a runtime error.
SQL
SELECT IdCol, Col1 / NULLIF(Col1 + Col2, 0) AS Col1Ratio FROM   dbo.SampleTable;

If so desired, a COALESCE function can be added to change this NULL result back to a chosen numeric value – or it can be left as NULL to make it instantly visible that for this row, a value of Col1Ratio could not be computed.


Refer: What if null if null is null null null is null? [^]
 
Share this answer
 
Comments
Monjurul Habib 31-Jul-12 13:45pm    
5+ :)
Sandeep Mewara 31-Jul-12 14:10pm    
Thanks.

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