-1

我的数据库维护不善,其中包含员工信息。人力资源部要求提供一份报告,列出与保险范围相关的员工姓名与保险单上的姓名不匹配的情况。

两个表中名称的格式不一致。它始终是姓氏,然后是名字,但您可能会在任一表格中看到名为 Steven J. Smith 的虚构员工的以下任何内容:

  1. 史密斯,史蒂文
  2. 史密斯,史蒂文
  3. 史密斯,史蒂文·J。
  4. 史密斯,史蒂文·J。

我需要运行一个查询来查找 EMPLOYEE.EMP_NAME <> INSURANCE.SUBSCRIBER_NAME 的实例,同时允许名称格式存在差异,如上所示(即选择“Smith,Steven J.”和“Smith,Steven”(可能)同一个人并与他们联系)。

SELECT 
  EMPLOYEE.EMP_NO
, EMPLOYEE.EMP_NAME
, INSURANCE.SUBSCRIBER_NAME
, INSURANCE.PAYOR_NAME

FROM EMPLOYEE
     INNER JOIN INSURANCE ON EMPLOYEE.EMP_NO = INSURANCE.EMP_NO

WHERE EMPLOYEE.EMP_NAME <> INSURANCE.SUBSCRIBER_NAME

我知道我想做一个子字符串来忽略中间的首字母,但是我该如何解释忽略逗号后是否有空格?

4

4 回答 4

0

你可以简单地replace去掉逗号

 WHERE replace (EMPLOYEE.EMP_NAME,',','') <> replace (INSURANCE.SUBSCRIBER_NAME,',','')

要找到大多数不匹配...

;with cE as 
(select 
     EMP_NO, 
     REPLACE(REPLACE(REPLACE(EMP_NAME,',',''),' ',''),'.','') as namekey 
from EMPLOYEE),
ci as 
(select 
     EMP_NO, 
     REPLACE(REPLACE(REPLACE(SUBSCRIBER_NAME,',',''),' ',''),'.','') as namekey 
from INSURANCE)
select *
from ce
    inner join ci on ce.EMP_NO = ci.EMP_NO
where
     not
     (
    (LEN(ce.namekey)< LEN(ci.namekey) and ci.namekey like ce.namekey+'%')
        or
    (LEN(ce.namekey)>= LEN(ci.namekey) and ce.namekey like ci.namekey+'%')
     )
于 2012-10-29T14:00:19.800 回答
0

为什么不使用 REPLACE 删除所有逗号和空格?

WHERE REPLACE(REPLACE(EMPLOYEE.EMP_NAME,' ',''),',','') <> REPLACE(REPLACE(INSURANCE.SUBSCRIBER_NAME,' ',''),',','')
于 2012-10-29T14:10:46.140 回答
0

您可以删除逗号后的空格,然后删除首字母

declare @Temp table (Name nvarchar(128))

insert into @Temp
select 'Smith, Steven' union all
select 'Smith,Steven' union all
select 'Smith, Steven J.' union all
select 'Smith,Steven J.'

select 
    case
        when N1.Name like '% %' then left(N1.Name, charindex(' ', N1.Name))
        else N1.Name
    end as Name_New,
    T.Name
from @Temp as T
    outer apply (select replace(T.Name, ', ', ',') as Name) as N1
于 2012-10-29T14:34:30.913 回答
0

谢谢,你的回答很有帮助。我最终将名字切割成 [lastname][firstname] 没有空格,如果中间的首字母在那里,我就把它剪掉。以下是最终消除绝大多数同名匹配的方法:

((CASE
WHEN CHARINDEX(' ',REPLACE(REPLACE(EMPLOYEE.EMP_NAME,', ',''),',','')) = 0
THEN UPPER(REPLACE(REPLACE(EMPLOYEE.EMP_NAME,', ',''),',',''))
ELSE UPPER(LEFT(REPLACE(REPLACE(EMPLOYEE.EMP_NAME,', ',''),',',''),CHARINDEX(' ',REPLACE(REPLACE(EMPLOYEE.EMP_NAME,', ',''),',',''))))
END) <> 
(CASE
WHEN CHARINDEX(' ',REPLACE(REPLACE(INSURANCE.SUBSCRIBER_NAME
,', ',''),',','')) = 0
THEN UPPER(REPLACE(REPLACE(INSURANCE.SUBSCRIBER_NAME
,', ',''),',',''))
ELSE UPPER(LEFT(REPLACE(REPLACE(INSURANCE.SUBSCRIBER_NAME
,', ',''),',',''),CHARINDEX(' ',REPLACE(REPLACE(INSURANCE.SUBSCRIBER_NAME
,', ',''),',',''))))
END))
于 2012-10-31T13:18:07.910 回答