Extract rows of a table, that include less than x NULLsWhat do these statements mean in the MS β exam 70-461 “skills measured” list?SQL SERVER 2008 TVF OR CHARINDEX to search column with commaHow can I do a differential query (delta plus/minus) telling me what rows are in view A that are not in view B and vice versa?Unique constraint on multiple nullable columns Sql ServerHow do I include nulls during comparisons in SQL Server?How do I include nulls during comparisons in SQLServer?I can't save Database DiagramsRecompile not working for DELETE statementPerformance gap between WHERE IN (1,2,3,4) vs IN (select * from STRING_SPLIT('1,2,3,4',','))Stored procedure or Table Function doesn't return value when parsing XML
Should I cover my bicycle overnight while bikepacking?
Expand and Contract
Which is the best way to check return result?
Avoiding the "not like other girls" trope?
What is a romance in Latin?
Could the museum Saturn V's be refitted for one more flight?
ssTTsSTtRrriinInnnnNNNIiinngg
Plagiarism or not?
Are there any examples of a variable being normally distributed that is *not* due to the Central Limit Theorem?
Mathematica command that allows it to read my intentions
Can I run a new neutral wire to repair a broken circuit?
How to tell a function to use the default argument values?
How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?
What mechanic is there to disable a threat instead of killing it?
Would Slavery Reparations be considered Bills of Attainder and hence Illegal?
Is "remove commented out code" correct English?
How can saying a song's name be a copyright violation?
Is it possible to create a QR code using text?
Why didn't Boeing produce its own regional jet?
Short story with a alien planet, government officials must wear exploding medallions
Personal Teleportation: From Rags to Riches
iPad being using in wall mount battery swollen
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
How seriously should I take size and weight limits of hand luggage?
Extract rows of a table, that include less than x NULLs
What do these statements mean in the MS β exam 70-461 “skills measured” list?SQL SERVER 2008 TVF OR CHARINDEX to search column with commaHow can I do a differential query (delta plus/minus) telling me what rows are in view A that are not in view B and vice versa?Unique constraint on multiple nullable columns Sql ServerHow do I include nulls during comparisons in SQL Server?How do I include nulls during comparisons in SQLServer?I can't save Database DiagramsRecompile not working for DELETE statementPerformance gap between WHERE IN (1,2,3,4) vs IN (select * from STRING_SPLIT('1,2,3,4',','))Stored procedure or Table Function doesn't return value when parsing XML
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);
sql-server query isnull
New contributor
add a comment |
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);
sql-server query isnull
New contributor
add a comment |
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);
sql-server query isnull
New contributor
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);
sql-server query isnull
sql-server query isnull
New contributor
New contributor
edited 6 hours ago
MDCCL
6,85331745
6,85331745
New contributor
asked 8 hours ago
sqlNewiesqlNewie
152
152
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You should use a case statement like this:
SELECT *
FROM test123
WHERE (
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
< 2);
The ISNULL
approach is returning your actual values when the value isn't NULL
, which pushes all of the rows over the 2 mark.
add a comment |
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
c1 float,
c2 float,
c3 float,
c4 float,
c5 float
);
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);
To see why ISNULL
isn't effective here, run this query:
SELECT ISNULL(c1,1), ISNULL(c2,1), ISNULL(c3,1), ISNULL(c4,1), ISNULL(c5,1)
FROM #test123;
You've given every column in every row a value. So now you're evaluating the SUM of inflated values, and erroneously evaluating a property of the actual value (what happens when one of the values is negative?), instead of evaluating the COUNT of values that either are NULL or are NOT NULL.
It's more code but a simple way to address this is:
SELECT * FROM #test123
WHERE CASE WHEN c1 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c2 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c3 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c4 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c5 IS NULL THEN 1 ELSE 0 END < 2;
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
sqlNewie is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233861%2fextract-rows-of-a-table-that-include-less-than-x-nulls%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use a case statement like this:
SELECT *
FROM test123
WHERE (
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
< 2);
The ISNULL
approach is returning your actual values when the value isn't NULL
, which pushes all of the rows over the 2 mark.
add a comment |
You should use a case statement like this:
SELECT *
FROM test123
WHERE (
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
< 2);
The ISNULL
approach is returning your actual values when the value isn't NULL
, which pushes all of the rows over the 2 mark.
add a comment |
You should use a case statement like this:
SELECT *
FROM test123
WHERE (
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
< 2);
The ISNULL
approach is returning your actual values when the value isn't NULL
, which pushes all of the rows over the 2 mark.
You should use a case statement like this:
SELECT *
FROM test123
WHERE (
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
< 2);
The ISNULL
approach is returning your actual values when the value isn't NULL
, which pushes all of the rows over the 2 mark.
answered 8 hours ago
Josh DarnellJosh Darnell
7,62022241
7,62022241
add a comment |
add a comment |
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
c1 float,
c2 float,
c3 float,
c4 float,
c5 float
);
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);
To see why ISNULL
isn't effective here, run this query:
SELECT ISNULL(c1,1), ISNULL(c2,1), ISNULL(c3,1), ISNULL(c4,1), ISNULL(c5,1)
FROM #test123;
You've given every column in every row a value. So now you're evaluating the SUM of inflated values, and erroneously evaluating a property of the actual value (what happens when one of the values is negative?), instead of evaluating the COUNT of values that either are NULL or are NOT NULL.
It's more code but a simple way to address this is:
SELECT * FROM #test123
WHERE CASE WHEN c1 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c2 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c3 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c4 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c5 IS NULL THEN 1 ELSE 0 END < 2;
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
add a comment |
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
c1 float,
c2 float,
c3 float,
c4 float,
c5 float
);
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);
To see why ISNULL
isn't effective here, run this query:
SELECT ISNULL(c1,1), ISNULL(c2,1), ISNULL(c3,1), ISNULL(c4,1), ISNULL(c5,1)
FROM #test123;
You've given every column in every row a value. So now you're evaluating the SUM of inflated values, and erroneously evaluating a property of the actual value (what happens when one of the values is negative?), instead of evaluating the COUNT of values that either are NULL or are NOT NULL.
It's more code but a simple way to address this is:
SELECT * FROM #test123
WHERE CASE WHEN c1 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c2 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c3 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c4 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c5 IS NULL THEN 1 ELSE 0 END < 2;
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
add a comment |
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
c1 float,
c2 float,
c3 float,
c4 float,
c5 float
);
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);
To see why ISNULL
isn't effective here, run this query:
SELECT ISNULL(c1,1), ISNULL(c2,1), ISNULL(c3,1), ISNULL(c4,1), ISNULL(c5,1)
FROM #test123;
You've given every column in every row a value. So now you're evaluating the SUM of inflated values, and erroneously evaluating a property of the actual value (what happens when one of the values is negative?), instead of evaluating the COUNT of values that either are NULL or are NOT NULL.
It's more code but a simple way to address this is:
SELECT * FROM #test123
WHERE CASE WHEN c1 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c2 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c3 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c4 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c5 IS NULL THEN 1 ELSE 0 END < 2;
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
c1 float,
c2 float,
c3 float,
c4 float,
c5 float
);
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);
To see why ISNULL
isn't effective here, run this query:
SELECT ISNULL(c1,1), ISNULL(c2,1), ISNULL(c3,1), ISNULL(c4,1), ISNULL(c5,1)
FROM #test123;
You've given every column in every row a value. So now you're evaluating the SUM of inflated values, and erroneously evaluating a property of the actual value (what happens when one of the values is negative?), instead of evaluating the COUNT of values that either are NULL or are NOT NULL.
It's more code but a simple way to address this is:
SELECT * FROM #test123
WHERE CASE WHEN c1 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c2 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c3 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c4 IS NULL THEN 1 ELSE 0 END
+ CASE WHEN c5 IS NULL THEN 1 ELSE 0 END < 2;
answered 8 hours ago
Aaron Bertrand♦Aaron Bertrand
153k18298493
153k18298493
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
add a comment |
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
Thank you a lot! The #temp table will help me a lot in the future:)!
– sqlNewie
8 hours ago
add a comment |
sqlNewie is a new contributor. Be nice, and check out our Code of Conduct.
sqlNewie is a new contributor. Be nice, and check out our Code of Conduct.
sqlNewie is a new contributor. Be nice, and check out our Code of Conduct.
sqlNewie is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233861%2fextract-rows-of-a-table-that-include-less-than-x-nulls%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown