Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Replace Null with 0

Author  Topic 

abhi123
Starting Member

10 Posts

Posted - 2009-05-28 : 12:35:46
Hi
i am new in sql. what i am doing is couting grades. For a paticular course of a student i am counting his/her grade. For simplicity grade can be either P or F. The data i have it is possible that a student would not get any grade for that course. So when i put count on that student for which he didn't get the grade query display null value. Is there any way that null can be replaced with 0.
 
student_name grades
A P
B
C F
D P
E
F P

so B and E the system will display null but i want it 0..
Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-28 : 12:43:44
[code]
SELECT student_name,count(*)
FROM Table
GROUP BY sudent_name
[/code]
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-05-28 : 12:50:33
Your question says "count" of grades but your sample shows the actual grade values (P and F). Visakh16's solution will show what you asked for. But to display a replacement for NULL you can use the isnull function:
isNull(grades, '0') as grades

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-28 : 13:13:36
or COALESCE(grades, 0)
i gave count(*) as in fact what you want is to count null grades also i.e just number of occurance of a particular student_name in table
but strictly speaking the answer to your question is ISNULL(grades, 0) or COALESCE(grades, 0)
Go to Top of Page

abhi123
Starting Member

10 Posts

Posted - 2009-05-28 : 13:16:48
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-28 : 13:18:43
welcome
Go to Top of Page
   

- Advertisement -