Access 2013 SQL: “IS NOT LIKE” issue

♀尐吖头ヾ 提交于 2019-12-11 03:15:12

问题


I cannot figure out why my WHERE clause seems to still return column values where the value is "SPEC"... what am I doing wrong here? [H1 Last Name] is the only column which contains this designation/value.

SELECT [H1 LAST Name] & ", " & [H1 FIRST Name] AS [FULL Name],
       [H1 E-Mail] AS [E-Mail],
       IIF([H1 Cell Phone] IS NULL, [Home Phone], [H1 Cell Phone]) AS Phone
FROM   NameLookup
WHERE  ((NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%'))
  AND  (NameLookup.[H1 LAST Name] IS NOT NULL)

UNION ALL

SELECT [H2 LAST Name] & ", " & [H2 FIRST Name] AS [FULL Name],
       [H2 E-Mail] AS [E-Mail],
       IIF([H2 Cell Phone] IS NULL, [Home Phone], [H2 Cell Phone]) AS Phone
FROM   NameLookup
WHERE  ((NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%'))
  AND  (NameLookup.[H2 LAST Name] IS NOT NULL)

ORDER BY [FULL NAME]

回答1:


The wild card characters for LIKE are different depending on the context where the query runs.

From ADO, the wild cards are % and _:

NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%'

From DAO, the wild cards are * and ?:

NameLookup.[H1 LAST Name] NOT LIKE '*SPEC*'

Or you could use ALIKE instead of LIKE, and then the db engine will always expect % and _ wild cards regardless of the context where the query runs:

NameLookup.[H1 LAST Name] NOT ALIKE '%SPEC%'



回答2:


Your query accepts records where

(NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%')

so if the last name does not contain 'MODEL' but does contain 'SPEC' it will make it into your results.



来源:https://stackoverflow.com/questions/22257034/access-2013-sql-is-not-like-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!