473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL Statement Count NULL Values

Hello,

Thanks for helping me with this... I really appreciate it.

I have a table called tblPatientDemographics with a number of columns.
I would like to count the number of NULL values per record within my
table.

tblPatientDemographics
PatientID Age Weight Height Race
1234567 20 155 <NULL> Caucasian
8912345 21 <NULL> <NULL> <NULL>

In the first example above I want to display '1'
In the second example above I want to display '3'

Any help would be very much appreciated.

Thanks !

Chad

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
3 19148
Here are two possibilities:

SELECT
CASE WHEN patientid IS NULL THEN 1 ELSE 0 END+
CASE WHEN age IS NULL THEN 1 ELSE 0 END+
CASE WHEN weight IS NULL THEN 1 ELSE 0 END+
CASE WHEN height IS NULL THEN 1 ELSE 0 END+
CASE WHEN race IS NULL THEN 1 ELSE 0 END
FROM tblPatientDemographics

SELECT 5-COUNT(patientid)-COUNT(age)-COUNT(weight)-COUNT(height)-COUNT(race)
FROM tblPatientDemographics
GROUP BY <primary key column(s)>

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
Hello,

Thanks for your two suggestions. But is there a way to count the NULL
values using * ? Count(*) WHERE IS NULL ? I want to avoid listing out
each column name because there is probably about 20 or so columns in
that table.

Thanks,

Chad

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
There isn't a way to do it without listing the columns unless you resort to
dynamic SQL. For reasons of reliability and ease of maintenance it's
generally a good idea to list columns by name in a query. Avoid using SELECT
* in production code. Query Analyzer lets you drag a list of column names
from the Object Browser into the query window, which can save some typing
effort.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into...
4
by: Spark | last post by:
Hi, Situation: Need a query to return number of ticket records by month of open in a log table where the ticket open record is older than 24 hours then the ticket pending or ticket closed...
3
by: Narine | last post by:
Hi All, I need to write one complicated update statement and I'm looking at maybe finding a simpler way to do it. I have 2 tables: 1.Photo Table PhotoID FileName 1 111.jpg
9
by: Vorpal | last post by:
Here is a small sample of data from a table of about 500 rows (Using MSSqlserver 2000) EntryTime Speed Gross Net ------------------ ----- ----- 21:09:13.310 0 0 0 21:09:19.370 9000 ...
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
0
by: rajkumargnv | last post by:
I know there are differences in MERGE Statement in UDB and z/OS like in z/Os only one 'WHEN MATCHED', one 'WHEN NOT MATCHED' are allowed. My question is more about USING clause, does it always...
0
by: kishoramballi | last post by:
Hi, I have created table using following sql statement through psql tool in test database using create table t1(f1 int); table is created. Now I am using an odbc script to insert 5 records....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.