I'm trying to create a query that will return all the rows that have a null value across all but 1 column. Some rows will have more than one null entry somewhere. There is one column I will want to exclude, because at this moment in time all of the entries are null and it is the only column that is allowed to have null values. I am stuck because I don't know how to include all of the columns in the WHERE.
SELECT *
FROM Analytics
WHERE * IS NULL
Alternatively, I can do a count for one column, but the table has about 67 columns.
SELECT COUNT(*)
FROM Analytics
WHERE P_Id IS NULL
In SQL Server you can borrow the idea from this answer
SQL Fiddle
Likely constructing a query with 67 columns will be more efficient but it saves some typing or need for dynamic SQL to generate it.
I don't have such a table to test, assuming there is no
'x'
as data in any field, I think this should work onSql-Server
; (DEMO)NOTE: I have filtered keyColumn as
c.name != 'keyColumn'
Depending on which RDBMS you're using, I think your only option (rather than explicitly saying
WHERE col1 IS NULL and col2 IS NULL and col3 IS NULL
...) would be to use Dynamic SQL.For example, if you want to get all the column names from a SQL Server database, you could use something like this to return those names:
You could use FOR XML to create your WHERE clause:
Hope this helps get you started.
Good luck.