user6322087 user6322087 - 2 years ago 123
SQL Question

Counting empty or null fields in SQL

I need to count how many empty fields a row contains in an SELECT COUNT(*) statement

My row holds 11 fields and i only need to count in 4 of them.
In two of the columns i need to count if they are empty (NOT NULL), and in two i need to count if they hold the value 0

My statement so far:

SELECT COUNT(*) AS subjectcount FROM Tabel WHERE (col1 OR col2) =0 OR (col3 OR col4) = '' AND id=1

Lets say that
col1 = 0
col2 = 1
col3=' '
my sum should then be 2, since two of the fields holds the value im searching for.

Answer Source
WHERE 0 in (col1, col2)
  OR '' in (col3, col4)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download