Search code examples
sqlssms-2012

comparing a row and including the null


I have a column that has numbers like this

p123
p139
d291
c9384
p393
null
null
c148

I want to compare this column where it says it includes only the p and null values. My code is:

WHERE (COLUMN NOT LIKE 'C%' AND COLUMN NOT LIKE 'D%' OR COLUMN IS NULL)

WHEN I have the condition COLUMN NOT LIKE 'C%' AND COLUMN NOT LIKE D% it comes back with out the null, but when I add the is null value, it comes back with one a c column even though there is a comparison to not include ones with c. I don't know how to get the order right or what parenthesis to use.


Solution

  • You could use a wildcard:

    where [column] not like '[cd]%' or [column] is null
    

    this would exclude any rows beginning with eithercord. In your example the three rows starting withpand the two rows withnullwould be returned.