• 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/