简体   繁体   English

SQL Replace查询从名称包含(MR。,MRS。,MR / MRS…)中删除前缀

[英]SQL Replace query to remove prefix from name contains (MR., MRS., MR / MRS…)

i have a table of content like this 我有这样的内容表

tblEmployees 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

and Another table like this 另一个像这样的桌子

tblPrefix 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

Now i would like to remove prefix (JR, JR., MR, MRS.....) present in EmployeeName. 现在我想删除EmployeeName中存在的前缀(JR,JR。,MR,MRS .....)。

i have written a function. 我写了一个函数。 For that function i have passed Employee name as parameter like this 对于该功能,我已经将Employee名称作为参数传递给了

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

and i tried like this 我尝试过这样

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

expected outPut 预期输出

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

Compare with first tblEmpoyee 与第一个tblEmpoyee进行比较

Oracle Query: Oracle查询:

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

In SQL-Server , I think it should be: SQL Server中 ,我认为应该是:

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

The following query selects employeeNames that start or end with a prefix. 以下查询选择以前缀开头或结尾的employeeNames。 Then, the prefix is stripped off the employeeName using the SUBSTRING function. 然后,使用SUBSTRING函数将前缀删除employeeName。

EDIT : Corrected the CASE statement. 编辑 :更正了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+' '+'%';

The above query would not unintentionally replace prefix characters that occur in the middle of the employeeName. 上面的查询不会无意中替换在employeeName中间出现的前缀字符。

SQL Fiddle demo SQL Fiddle演示

You can embed the SQL statement in a function, as below. 您可以将SQL语句嵌入到函数中,如下所示。 However, please note that the function would perform slower, as it is executed for each employeeName one by one. 但是,请注意,该函数的执行速度会较慢,因为它会针对每个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;

Reference : 参考

Create Function on MSDN 在MSDN上创建函数

Your query can also be changed as: 您的查询也可以更改为:

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

You need to handle scenarios where name may be in following cases: 在以下情况下,您需要处理可能出现名称的情况:

  • '(space)PREFIX(space)' --> Name containing prefix in between surrounded by spaces. '(space)PREFIX(space)' ->名称之间由空格包围的前缀。

    Example: Jeffrey L. JR Van Hoosear 例如:Jeffrey L. JR Van Hoosear

  • 'PREFIX(space)' --> Name beginning with prefix followed by space followed by name 'PREFIX(space)' ->名称以前缀开头,后跟空格,然后是名称

    Example: JR Jeffrey 例如:JR Jeffrey

  • '(space)PREFIX' --> Name ending with space and prefix '(space)PREFIX' ->以空格和前缀结尾的名称

    Example: Jeffrey JR 例如:Jeffrey JR

  • 'PREFIX' --> Without space 'PREFIX' ->没有空格

    Example: JR 示例:JR

The following 下列

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

which does not resolve " JR " was produced by: 不能解决“ JR”的原因是:

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)

see this sqlfiddle 看到这个sqlfiddle

It would also fail on "MR / MRS" 在“ MR / MRS”上也会失败

Use this query in your function. 在您的函数中使用此查询。 IT will remove Prefix appearing at start, end and in middle IT将删除在开头,结尾和中间出现的前缀

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM