简体   繁体   中英

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

i have a table of content like this

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

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.

i have written a function. For that function i have passed Employee name as parameter like this

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

Oracle Query:

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

In SQL-Server , I think it should be:

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. Then, the prefix is stripped off the employeeName using the SUBSTRING function.

EDIT : Corrected the CASE statement.

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.

SQL Fiddle demo

You can embed the SQL statement in a function, as below. However, please note that the function would perform slower, as it is executed for each employeeName one by one.

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

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.

    Example: Jeffrey L. JR Van Hoosear

  • 'PREFIX(space)' --> Name beginning with prefix followed by space followed by name

    Example: JR Jeffrey

  • '(space)PREFIX' --> Name ending with space and prefix

    Example: Jeffrey JR

  • 'PREFIX' --> Without space

    Example: 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:

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

It would also fail on "MR / MRS"

Use this query in your function. IT will remove Prefix appearing at start, end and in middle

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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