Sample Header Ad - 728x90

Extract rows of a table, that include less than x NULLs

4 votes
2 answers
403 views
I am working with a SQL Server database, which includes a lot of NULLs. To analyse my data, I want to extract all rows of the database table, that include less than x NULL marks (e.g. x=2). My database is similar to this structure: c1 c2 c3 c4 c5 ----------------------------------------------------- 2 3 NULL 1 2 2 NULL NULL 1 2 2 3 NULL NULL 2 NULL 3 NULL 1 NULL 2 3 NULL 1 2 I tried the query, which doesn't return an error, but no rows are selected: SELECT * FROM test123 WHERE ((ISNULL(c1,1) + ISNULL(c2,1) + ISNULL(c3,1) + ISNULL(c4,1) + ISNULL(c5,1)) < 2); I expect this query to return the 1st and the fifth row, but the result contains 0 rows. --------------------------- I can't test the following code, because I don't have the rights to write on the database, but here is a (pseudo-) code for creating a table like mine: CREATE TABLE test123( c1 float, c2 float, c3 float, c4 float, c5 float ) GO INSERT test123(c1,c2,c3,c4,c5) VALUES (2,3,NULL,1,2), (2,NULL,NULL,1,2), (2,3,NULL,NULL,2), (NULL,3,NULL,1,NULL), (2,3,NULL,1,2);
Asked by sqlNewie (53 rep)
Apr 3, 2019, 04:57 PM
Last activity: Apr 3, 2019, 07:23 PM