SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


code help where clause


code help where clause

Author
Message
SQL_Surfer
SQL_Surfer
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 1086
What is the correct way of coding this WHERE clause?

By putting or, I am getting some records with the exclusion values.

If replace or with AND in the second line, I won't get the exlcusion values but I won't get where a.col1/d.col2 is NULL. I want all the records where a.col1 or d.col2 is NULL and all other values except the ones specified in the exclusion list.

AND isnull(a.col1, ' ') NOT IN ('abc', 'def', 'ghi'))
or (d.col2 is null or isnull(d.col2,' ') NOT IN ('abc', 'def', 'ghi'))
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16587 Visits: 19098
I don't get it. Could you please post sample data and expected results? Check the article linked on my signature to guide you.
Your description and the results seem to match, but I might be misunderstanding the whole problem.
This is what I used to test.
WITH CTE(col1, col2) AS(
SELECT 'abc' col1, 'bbc' col2 UNION ALL
SELECT 'nbc' col1, 'abc' col2 UNION ALL
SELECT NULL col1, NULL col2 )
SELECT *
FROM CTE
WHERE ISNULL( col1, '') NOT IN ('abc', 'def', 'ghi')
OR (col2 is null or isnull(col2,' ') NOT IN ('abc', 'def', 'ghi'))




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26135 Visits: 17539
There really is no need for the ISNULL checks here. NULL is not in any of those value either. All the ISNULL check does here is makes it a nonSARGable predicate.


AND a.col1 NOT IN ('abc', 'def', 'ghi')
or (d.col2 is null or d.col2 NOT IN ('abc', 'def', 'ghi'))



I have to agree with Luis thought that without some clarification it is hard to know what you want here.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQL_Surfer
SQL_Surfer
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 1086
Row with abc shouldn't show up since abc is the exclusion list. On yours it is appearing twice. In row 1 and 2.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16587 Visits: 19098
Sean Lange (7/24/2013)
There really is no need for the ISNULL checks here.

There's a need for a NULL validation since the OP wants to show NULLs as well.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
SQL_Surfer
SQL_Surfer
SSChasing Mays
SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)SSChasing Mays (651 reputation)

Group: General Forum Members
Points: 651 Visits: 1086
WITH CTE (col1, col2, col3) AS
(
SELECT '1' col1,'abc' col2, 'bbc' col3 UNION ALL
SELECT '2'col1,'nbc' col2, 'zhi' col3 UNION ALL
SELECT '3'col1, NULL col2, 'abc' col3 UNION ALL
SELECT '4'col1, 'gef' col2, NULL col3 UNION ALL
SELECT '5'col1, NULL col2, NULL col3
)
SELECT *
FROM CTE


On this one, it should only show up row 2, 4 and 5. I want to keep if col2 or col3 is null but they need to be exluded if they have abc, def or ghi in them.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26135 Visits: 17539
Luis Cazares (7/24/2013)
Sean Lange (7/24/2013)
There really is no need for the ISNULL checks here.

There's a need for a NULL validation since the OP wants to show NULLs as well.


You are right Luis. I misread that entirely. I really should learn to read the whole post before jumping to conclusions. Hehe

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26135 Visits: 17539
SQL_Surfer (7/24/2013)
WITH CTE (col1, col2, col3) AS
(
SELECT '1' col1,'abc' col2, 'bbc' col3 UNION ALL
SELECT '2'col1,'nbc' col2, 'zhi' col3 UNION ALL
SELECT '3'col1, NULL col2, 'abc' col3 UNION ALL
SELECT '4'col1, 'gef' col2, NULL col3 UNION ALL
SELECT '5'col1, NULL col2, NULL col3
)
SELECT *
FROM CTE


On this one, it should only show up row 2, 4 and 5. I want to keep if col2 or col3 is null but they need to be exluded if they have abc, def or ghi in them.


Like this?


SELECT *
FROM CTE
where (col2 is null or col2 not in ('abc', 'def', 'ghi'))
AND
(col3 is null or col3 not in ('abc', 'def', 'ghi'))



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search