标准字符串将是“First(Pref)Last”,但有些具有不同的格式,例如没有首选名称的“First Last”。其他人有两个名字“First First(Pref)Last”,一个在括号和姓氏“First(Pref)Last”之间缺少一个空格。我知道如何做First(Pref)Last和First Last,但最后两个例子让我查询了。这是前两个代码。
UPDATE [dbo].[NameTester]
SET FName = PARSENAME(REPLACE(FullName,' ','.'),3),
PrefName = PARSENAME(REPLACE(FullName,' ','.'),2),
LName = PARSENAME(REPLACE(Fullname,' ','.'),1)
where FullName like '% % %'
UPDATE [dbo].[NameTester]
SET FName = PARSENAME(REPLACE(FullName,' ','.'),2),
LName = PARSENAME(REPLACE(Fullname,' ','.'),1)
where FullName not like '% % %'
答案 0 :(得分:0)
我建议您不要使用parsename()
。而是在parens上休息。
UPDATE [dbo].[NameTester]
SET FName = TRIM(LEFT(FullName, charindex('(', FullName) - 1)),
PrefName = SUBSTRING(LEFT(FullName, charindex(')', FullName) - 1), charindex('(', FullName) + 1, len(FullName)),
LName = TRIM(RIGHT(FullName, charindex(')', reverse(FullName)) - 1))
where FullName like '%(%)%';
以下是使用select
(也在SQL Fiddle上)的示例:
select trim(LEFT(FullName, charindex('(', FullName) - 1)),
SUBSTRING(LEFT(FullName, charindex(')', FullName) - 1), charindex('(', FullName) + 1, len(FullName)),
trim(RIGHT(FullName, charindex(')', reverse(FullName)) - 1))
from (select 'a (b) c' as fullname union all
select 'abc (def)ghi' union all
select 'abc def (ghijkl) kkk'
) t