如何在角色后的第一个单词中获得第一个单词? 使用下面的查询
select
right(member_name, len(member_name) - charindex(' ', member_name)) as first_name,
left(member_name, charindex('/', member_name) - 1) + ' ' as last_name
from
member
我得到了正确的last_name,但是对于名字,我得到了全名 我只想在' /'之后得到第一个字。字符。
在表中,collumn的名字类似于&#;; smith / john doe' 所以我希望输出john(名字)smith(last_name)
答案 0 :(得分:1)
这是一个更好的解决方案 - 检查成员是否有中间名:
select
case
when charindex(' ', member_name) > 0 then
SUBSTRING(member_name, charindex('/', member_name) + 1, charindex(' ', member_name) - charindex('/', member_name))
when charindex(' ', member_name) < 1 then
SUBSTRING(member_name, charindex('/', member_name) + 1, len(member_name) - charindex('/', member_name))
end as FIRST_NAME,
left(member_name, charindex('/', member_name) - 1) + ' ' as last_name
from
member
我更新了sql fiddle here