SQL替换查询以从名称中删除前缀包含(MR。,MRS。,MR / MRS .....)

时间:2014-07-10 04:49:49

标签: sql sql-server sql-server-2008

我有一个像这样的内容表

tblEmployees

employeeID       employeeName
___________________________________
    1       Jeffrey L. JR Van Hoosear
    2       DAVID GUNGNER MR
    3       CATHLEEN E STADECKER MRS.
    4       MARTIN W SCHIFFMILLER
    5       JAY F MOLDOVANYI VI

和另一张像这样的表

tblPrefix


prefixID       Prefix
_________________________
    1           JR
    2           MR
    3           MR / MRS
    4           JR.
    5           MRS.
    6           I
    7           II
    8           III
    9           IV
   10           V
   11           VI
   12           VII

现在我想删除EmployeeName中的前缀(JR,JR。,MR,MRS .....)。

我写了一个函数。 对于该函数,我已将Employee名称作为参数传递给此

SELECT * FROM fn_SplitName (@employeeName) (Table-Valued Function)

我试过这个

SELECT REPLACE(@employeeName,preFix,'') 
FROM tblPrefix 
WHERE @employeeName LIKE '% ' + preFix + ' %'

预期outPut

employeeID       employeeName
___________________________________
    1       Jeffrey L. Van Hoosear
    2       DAVID GUNGNER
    3       CATHLEEN E STADECKER
    4       MARTIN W SCHIFFMILLER
    5       JAY F MOLDOVANYI

与第一个tblEmpoyee比较

5 个答案:

答案 0 :(得分:2)

Oracle查询:

select employeeName, REPLACE(employeeName, PREFIX,'')  
from employee_table, prefix_table
WHERE INSTR(employeeName, PREFIX) > 0

SQL-Server 中,我认为它应该是:

select employeeName, REPLACE(employeeName, PREFIX,'')  
from employee_table, prefix_table
WHERE CHARINDEX(PREFIX,employeeName) > 0

答案 1 :(得分:1)

以下查询选择以前缀开头或结尾的employeeNames。然后,使用SUBSTRING函数从employeeName中删除前缀。

修改: 更正了CASE声明。

SELECT te.employeeName, 
    CASE
        WHEN te.employeeName like '%'+' '+tp.Prefix THEN SUBSTRING(te.employeeName, 1, LEN(te.employeeName)-LEN(tp.Prefix)-1)
        WHEN te.employeeName like tp.Prefix+' '+'%' THEN SUBSTRING(te.employeeName, LEN(tp.Prefix)+2, LEN(te.employeeName)-LEN(tp.Prefix)-1)
    END employeeName_without_Prefix
FROM tblEmployees te
INNER JOIN tblPrefix tp ON te.employeeName like '%'+' '+tp.Prefix OR te.employeeName like tp.Prefix+' '+'%';

上述查询不会无意中替换employeeName中间出现的前缀字符。

SQL Fiddle demo

您可以将SQL语句嵌入到函数中,如下所示。但请注意,该函数执行速度较慢,因为它是逐个为每个employeeName执行的。

CREATE FUNCTION dbo.remove_prefix (@employeeName varchar(100))
RETURNS varchar(100)
AS
BEGIN
    DECLARE @employeeName_without_Prefix   varchar(100)

SELECT @employeeName_without_Prefix = 
    CASE
        WHEN te.employeeName like '%'+' '+tp.Prefix THEN SUBSTRING(te.employeeName, 1, LEN(te.employeeName)-LEN(tp.Prefix)-1)
        WHEN te.employeeName like tp.Prefix+' '+'%' THEN SUBSTRING(te.employeeName, LEN(tp.Prefix)+2, LEN(te.employeeName)-LEN(tp.Prefix)-1)
    END employeeName_without_Prefix
FROM tblEmployees te
INNER JOIN tblPrefix tp ON te.employeeName like '%'+' '+tp.Prefix OR te.employeeName like tp.Prefix+' '+'%';
     RETURN (@employeeName_without_Prefix);
END;

<强>参考

Create Function on MSDN

答案 2 :(得分:0)

您的查询也可以更改为:

SELECT REPLACE(@employeeName,preFix,'') 
FROM tblPrefix 
WHERE 
(
@employeeName LIKE '% ' + preFix + ' %'
OR @employeeName LIKE '%' + preFix + ' %'
OR @employeeName LIKE '% ' + preFix + '%'
OR @employeeName LIKE '%' + preFix + '%'
);

您需要处理以下情况可能出现名称的情况:

  • '(space)PREFIX(space)' - &gt;名称中包含由空格包围的前缀。

    示例:Jeffrey L. JR Van Hoosear

  • 'PREFIX(space)' - &gt;名称以前缀开头,后跟空格,后跟名称

    示例:JR Jeffrey

  • '(space)PREFIX' - &gt;名称以空格和前缀

    结尾

    示例:Jeffrey JR

  • 'PREFIX' - &gt;没有空间

    示例:JR

答案 3 :(得分:0)

以下

| Jeffrey L. JR Van Hoosear |
|         GEORGE WASHINGTON |
|           MARTHA D CUSTIS |
|          JOHN VON NEUMANN |
|          ROBERT G MONDAVI |

无法解决&#34; JR&#34;由以下人员制作:

select
        ca3.employeeName
from tblEmployees 
cross apply (
             select
                    charindex(' ',employeeName)
                  , charindex(' ',reverse(employeeName))
                  , len(employeeName)
            ) ca1 (posl, posr, wide)
cross apply (
             select 
                    case when posl > 1 then left(employeeName,posl-1) end
                  , case when posr > 1 then right(employeeName,posr-1) end
            ) ca2 (bitl, bitr)
cross apply (
             select 
                    case
                         when bitl in (select prefix from tblPrefix) then
                                 substring(employeeName,posl+1,wide)
                         when bitr in (select prefix from tblPrefix) then
                                 substring(employeeName,1,wide - posr)
                         else
                              employeeName
                    end
            ) ca3 (employeeName)

请参阅this sqlfiddle

MR / MRS&#34;

也会失败

答案 4 :(得分:0)

在您的函数中使用此查询。 IT将删除出现在开头,结尾和中间的前缀

SELECT REPLACE(@employeeName,preFix,'') 
FROM tblPrefix 
WHERE @employeeName LIKE '% ' + preFix + ' %'
OR @employeeName LIKE '% ' + preFix
OR @employeeName LIKE preFix + ' %'