开发者

ms access 2007 SQL "not like" is ignoring blank cells

I'm confused by some SQL I am trying to run. Here's a sample dataset

Username ID        Text1
red55   1235342     fdfdf
black   5542121     sdsd
red32   5542145     sffrds
green   5542911         
bluee   5333121     ffds

So the SQL would be

SELECT username, ID, Text1 
  FROM user_info
 WHERE username NOT LIKE 'red*' 
       AND text1 NOT LIKE 'sd*';

So I would expect to see a result set of

Username ID        Text1
green   5542911         
bluee   5333121     ffds

But it doesn't work. It seems to ignore the blank values. I would only see the one record (not like the two above). I need my SQL to simply say show me the records which dont start "red" AND also any records which don't start with "sd". Yet it either gets rid of them all AND blanks, or if I use an OR statement, gets rid of none.

开发者_StackOverflow中文版

Any ideas? I am stuck here!


You need to explicitly handle the nulls. One option would be:

SELECT username, ID, Text1 
FROM user_info 
WHERE (username Is Null OR username NOT LIKE 'red*')
  AND (text1 Is Null OR text1 NOT LIKE 'sd*')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜