Extract rows of a table, that include less than x NULLs [duplicate]Count where any 3 columns have values (not null)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
If I cast Expeditious Retreat, can I Dash as a bonus action on the same turn?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
What does CI-V stand for?
Font hinting is lost in Chrome-like browsers (for some languages )
What's the point of deactivating Num Lock on login screens?
The use of multiple foreign keys on same column in SQL Server
"You are your self first supporter", a more proper way to say it
Fencing style for blades that can attack from a distance
Is this a crack on the carbon frame?
Is it important to consider tone, melody, and musical form while writing a song?
Minkowski space
Dragon forelimb placement
TGV timetables / schedules?
What is the word for reserving something for yourself before others do?
Writing rule which states that two causes for the same superpower is bad writing
How can I make my BBEG immortal short of making them a Lich or Vampire?
Modeling an IPv4 Address
How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
What are the differences between the usage of 'it' and 'they'?
To string or not to string
Prove that NP is closed under karp reduction?
What do you call a Matrix-like slowdown and camera movement effect?
Why Is Death Allowed In the Matrix?
Extract rows of a table, that include less than x NULLs [duplicate]
Count where any 3 columns have values (not null)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
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Count where any 3 columns have values (not null)
1 answer
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
marked as duplicate by Paul White♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Count where any 3 columns have values (not null)
1 answer
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
marked as duplicate by Paul White♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Count where any 3 columns have values (not null)
1 answer
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
This question already has an answer here:
Count where any 3 columns have values (not null)
1 answer
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);
This question already has an answer here:
Count where any 3 columns have values (not null)
1 answer
sql-server query isnull
sql-server query isnull
New contributor
New contributor
edited Apr 3 at 19:23
MDCCL
6,85331745
6,85331745
New contributor
asked Apr 3 at 16:57
sqlNewiesqlNewie
283
283
New contributor
New contributor
marked as duplicate by Paul White♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Paul White♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
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;
add a comment |
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 Apr 3 at 17:07
Josh DarnellJosh Darnell
7,84022243
7,84022243
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;
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;
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 Apr 3 at 17:09
Aaron Bertrand♦Aaron Bertrand
154k18298493
154k18298493
add a comment |
add a comment |