这是将名称拆分为组件的一种 hacky 方法(SQL-Server 方言):
create table InputNames (InputName nvarchar(50));
insert into InputNames (InputName) values ('Smith, John');
insert into InputNames (InputName) values ('Mary Smith');
insert into InputNames (InputName) values ('Mark Jones');
insert into InputNames (InputName) values ('White, Barry');
insert into InputNames (InputName) values ('Damien Drybread, Esquire');
select
InputName,
case when charindex(',', inputname, 1) = 0 then
substring (inputname, 1, charindex (' ', inputname, 1) - 1)
else
substring (inputname, charindex (',', inputname, 1) + 2, 99)
end
as firstname,
case when charindex(',', inputname, 1) = 0 then
substring (inputname, charindex (' ', inputname, 1) + 1, 99)
else
substring (inputname, 1, charindex (',', inputname, 1) - 1)
end
as lastname
from inputnames
这是输出
InputName firstname lastname
Smith, John John Smith
Mary Smith Mary Smith
Mark Jones Mark Jones
White, Barry Barry White
Damien Drybread, Esquire Esquire Damien Drybread
...并且您可以看到,如果您的姓名中可以包含逗号而不是名字和姓氏之间的分隔符,则您需要处理一些极端情况。
类似的方法适用于 MS Access,但您必须使用“Iif”而不是 case 语句,以及“InStr”而不是 charindex。