查询WHERE仅字母字符

时间:2016-12-15 18:16:23

标签: sql excel odbc microsoft-query

我正在尝试过滤出我公司的Excel客户表中的数据。 我需要的三个字段是FIRST_NAMELAST_NAMECOMPANY_NAME

规则如下:

  • FIRST_NAMELAST_NAME必须NOTNULL
  • FIRST_NAMELAST_NAME必须只是字母
  • 如果COMPANY_NAMENOT NULL
  • ,则上述规则无关紧要

所以,重申要明确一点..客户必须有FIRST_NAMELAST_NAME(他们不能错过一个或两个),但是,如果他们有{{1}他们被允许没有COMPANY_NAME和/或FIRST_NAME

以下是一些示例数据,是否应保留数据:

LAST_NAME

以下是查询(删除了FIRST_NAME | LAST_NAME | COMPANY_NAME | Good customer? -----------|-----------|--------------|-------------------------------- Alex | Goodman | AG Inc. | Yes - All are filled out John | Awesome | | Yes - First and last are fine Cindy | | Cindy Corp. | Yes - Company is filled out | | Blank Spa | Yes - Company is filled out | | | No - Nothing is filled out Gordon | Mang#2 | | No - Last contains non-alphabet Jesse#5 | Levvitt | JL Inc. | Yes - Company is filled out Holly | | | No - No last or company names 子句中的某些字段):

SELECT

我也试过SELECT VR_CUSTOMERS.CUSTOMER_ID, VR_CUSTOMERS.FIRST_NAME, VR_CUSTOMERS.LAST_NAME, VR_CUSTOMERS.COMPANY_NAME, ... FROM DEV.VR_CUSTOMERS VR_CUSTOMERS WHERE ( LENGTH(NAME)>4 AND (UPPER(NAME) NOT LIKE UPPER('%delete%')) AND (COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND FIRST_NAME IS NOT NULL AND LAST_NAME IS NOT NULL AND FIRST_NAME LIKE '%^[A-z]+$%' AND LAST_NAME LIKE '%^[A-z]+$%')) ) 的正则表达式。我尝试过'%[^a-z]%'RLIKE,而不是REGEXP,这些似乎也没有用。

使用上述查询,结果仅显示带有LIKE的记录。

2 个答案:

答案 0 :(得分:1)

使用REGEXP_LIKE和正则表达式^[A-z]+$解决了问题。

以下是此修复后的WHERE子句:

WHERE (
LENGTH(NAME)>4 AND
(UPPER(NAME) NOT LIKE UPPER('%delete%')) AND
(COMPANY_NAME IS NOT NULL OR (COMPANY_NAME IS NULL AND REGEXP_LIKE(FIRST_NAME, '^[A-z]+$') AND REGEXP_LIKE(LAST_NAME, '^[A-z]+$')))
)

答案 1 :(得分:0)

鉴于您提及RLIKE和REGEXP,您似乎正在使用MySQL。在这种情况下,请尝试使用regular expression character class 'alpha'

的WHERE子句
WHERE 
      COMPANY_NAME is not null    -- COMPANY_NAME being present is the higher priority pass condition 
  or  ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied 
           FIRST_NAME is not null 
       and FIRST_NAME REGEXP '[[:alpha:]]+' 
       and LAST_NAME is not null 
       and LAST_NAME REGEXP '[[:alpha:]]+' 
      ) 

请记住,给定正则表达式,非空检查是多余的,因此WHERE子句将自身简化为:

WHERE 
      COMPANY_NAME is not null    -- COMPANY_NAME being present is the higher priority pass condition 
  or  ( -- but if COMPANY_NAME is not present, then the following conditions must be satisfied 
           FIRST_NAME REGEXP '[[:alpha:]]+' 
       and LAST_NAME REGEXP '[[:alpha:]]+' 
      )