Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
I have a stored procedure that I use and i want to average null coulmns: my stored procedure
SQL
ALTER procedure [dbo].[Technology] 
@portRef int 
as
SELECT avg(planned)as Planned,avg(achieved)  as Achieved 
FROM Port inner join Technology on Port.portID = Technology.portRef 
WHERE portRef =@portRef
OR Technology.ShowAll =@portRef


so i bind this stored procedure to a chart using datasource and when the column is null the c# code throw this error
Value was either too large or too small for a Decimal.
how can I handle my stored to avg those null column
Posted
Updated 12-Jul-12 22:41pm
v2
Comments
Sunny_Kumar_ 13-Jul-12 4:28am    
you can try checking not for null values... I mean like adding a cluase "<column> is not null" in WHERE filter.
AshishChaudha 13-Jul-12 9:36am    
mark it as your answer if your issue is solved, so other can refer to the solution
Sergey Alexandrovich Kryukov 18-Mar-13 10:56am    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

you can try case in stored procedure.

VB
case avg(planned)
when null then 0
else avg(planned)
end
 
Share this answer
 
Try:
SQL
SELECT 
AVG(ISNULL(planned, 0)) as Planned, 
avg(ISNULL(achieved,0)) as Achieved
FROM Port inner join Technology on Port.portID = Technology.portRef
WHERE portRef =@portRef OR Technology.ShowAll =@portRef
 
Share this answer
 
Comments
AshishChaudha 13-Jul-12 9:33am    
my 5!
kolisa 30-Jul-12 3:55am    
it still returns null value
First of all, you need to decide what a null value should mean to you: 0 or need to be skipped: avg(null,1,2)==1 or ==1.5
In the first case you can use COALESCE[^], like this:
SQL
SELECT avg(COALESCE(planned,0))as Planned,avg(COALESCE(achieved,0)) as Achieved...

In the second case you can filter selection with
SQL
... WHERE planned is not null

but this way you can calculate only one average per select. But this is no actual problem in a stored procedure.
 
Share this answer
 
Comments
kolisa 30-Jul-12 3:58am    
both dont work
Zoltán Zörgő 30-Jul-12 4:10am    
Both should work, you are doing something wrong
you can check the null check in your c# code before bind the datasource

or you can try the decode in your stored procedure.
eg:
decode(avg(planned), null,0,avg(planned))
 
Share this answer
 
Comments
kolisa 30-Jul-12 3:58am    
decode is unknown build in function
jeyamothi 30-Jul-12 4:49am    
you try in your stored procedure code not in c# code behind file.
jeyamothi 30-Jul-12 4:50am    
are you using sql or oracle
kolisa 30-Jul-12 4:56am    
Got it Thanks
jeyamothi 30-Jul-12 5:00am    
thank you for your rating

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