我想删除特殊字符表单名称,不包括称呼部分和需要分隔名字和姓氏和称呼。
Create table Employee_Name(Emp_Name varchar(20))
insert into Employee_Name values('Dr. Sana .Singh');
insert into Employee_Name values('Mrs. Ashley;');
insert into Employee_Name values('Mr. Simon-');
insert into Employee_Name values('Ms. Ale Martin galyx');
insert into Employee_Name values('Doctor. Blank Smith&');
I need output to be
Emp_Name Salutation First_Name Last_Name
Dr. Sana Singh Dr. Sana Singh
Mrs. Ashley Mrs. Ashley
Mr. Simon Mr. Simon
Ms. Ale Martin Ms. Ale Martin
Doctor. Blank Smith Doctor. Blank Smith
提前致谢
答案 0 :(得分:3)
分裂名称可能是一个滑坡。但是,如果您要查找前3个字符串,请考虑以下内容。易于扩展,我相信你可以看到模式1,2,3,...(我没有太多的想象力)
Select A.Emp_Name
,Salutation = IsNull(B.Pos1,'')
,First_Name = IsNull(B.Pos2,'')
,Last_Name = IsNull(B.Pos3,'')
From @Employee_Name A
Cross Apply (
Select Pos1 = xDim.value('/x[1]','varchar(max)')
,Pos2 = xDim.value('/x[2]','varchar(max)')
,Pos3 = xDim.value('/x[3]','varchar(max)')
From (Select Cast('<x>' + Replace([dbo].[udf-Str-Strip-Non-Alphanumeric](A.Emp_Name),' ','</x><x>')+'</x>' as XML) as xDim) A
) B
返回
Emp_Name Salutation First_Name Last_Name
Dr. Sana .Singh Dr Sana Singh
Mrs. Ashley; Mrs Ashley
Mr. Simon- Mr Simon
Ms. Ale Martin galyx Ms Ale Martin
Doctor. Blank Smith& Doctor Blank Smith
现在,剥离非ALPHA字符需要UDF
CREATE FUNCTION [dbo].[udf-Str-Strip-Non-Alphanumeric](@S varchar(max))
Returns varchar(max)
Begin
Declare @RetVal varchar(max) = ''
;with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@S),0)) Row_Number() over (Order By (Select NULL)) From cte1 a,cte1 b,cte1 c,cte1 d)
Select @RetVal = @RetVal + Substring(@S,N,1)
From cte2
Where Substring(@S,N,1) Like '[a-z,A-Z,0-9, ]'
Return Replace(@RetVal,' ',' ')
End
--Select [dbo].[udf-Str-Strip-Non-Alphanumeric]('some & text. /+= a and zip 02806') --Returns: some text a and zip 02806
答案 1 :(得分:1)
尝试此解决方案,希望这可以帮助您。
SELECT
Emp_Name
, Reverse(ParseName(Replace(Reverse(Emp_Name), '', ''), 1)) As [Salutation]
, Reverse(ParseName(Replace(Reverse(Emp_Name), '', ''), 2)) As [First_Name]
, Reverse(ParseName(Replace(Reverse(Emp_Name), '', ''), 3)) As [Last_Name]
FROM (Select Emp_Name from Employee_Name
) As [x]
输出:
如果您需要改进,请进行编辑。
答案 2 :(得分:0)
尝试如下:
DECLARE @EMP TABLE (ID INT IDENTITY(1,1),EMP_NAME VARCHAR(100),SALUTATION VARCHAR(100),FIRST_NAME VARCHAR(100),LAST_NAME VARCHAR(100),COMMON VARCHAR(100))
DECLARE @NAME VARCHAR(100),@ID INT
INSERT INTO @EMP (EMP_NAME,SALUTATION,COMMON) SELECT EMP_NAME,SUBSTRING(EMP_NAME,0,CHARINDEX(' ',EMP_NAME)+1),REPLACE(EMP_NAME,(SUBSTRING(EMP_NAME,0,CHARINDEX(' ',EMP_NAME)+1)),'') FROM EMPLOYEE_NAME
DECLARE C CURSOR FOR
SELECT COMMON,ID FROM @EMP
OPEN C
FETCH NEXT FROM C INTO @NAME,@ID
WHILE @@FETCH_STATUS=0
BEGIN
IF(CHARINDEX(' ',@NAME))>0
BEGIN
UPDATE @EMP SET FIRST_NAME=(SELECT SUBSTRING(@NAME,0,CHARINDEX(' ',@NAME)+1)) WHERE ID=@ID
UPDATE @EMP SET LAST_NAME=(REPLACE(@NAME,(SUBSTRING(@NAME,0,CHARINDEX(' ',@NAME)+1)),'')) WHERE ID=@ID
IF(CHARINDEX(' ',(SELECT LAST_NAME FROM @EMP WHERE ID=@ID)))>0
BEGIN
UPDATE @EMP SET LAST_NAME=(SELECT SUBSTRING(LAST_NAME,0,CHARINDEX(' ',LAST_NAME)+1) FROM @EMP WHERE ID=@ID) WHERE ID=@ID
END
END
ELSE
BEGIN
UPDATE @EMP SET FIRST_NAME=@NAME WHERE ID=@ID
END
FETCH NEXT FROM C INTO @NAME,@ID
END
CLOSE C
DEALLOCATE C
SELECT EMP_NAME,SALUTATION,FIRST_NAME,LAST_NAME FROM @EMP