简体   繁体   English

从ORACLE DB中的名称字符串中提取姓氏

[英]extracting last name from a name string in ORACLE DB

I am writing a small query to extract all last names from a bunch of Author name database. 我正在编写一个小查询,以从一堆作者姓名数据库中提取所有姓氏。 Names on file will contain first and middle name, or just first name. 文件中的名称将包含名字和中间名,或仅包含名字。

Ex: John Smith John T. Smith 例如:约翰·史密斯(John Smith)

So I cannot search purely by just after the first space... But I know for sure that the lastname should be from the END to the first space from the right side of the string. 因此,我不能仅在第一个空格之后进行搜索...但是我确定,姓氏应从END到字符串右侧的第一个空格。 I don't really care about first name. 我真的不在乎名字。

This is what I currently have... 这就是我目前拥有的...

select [name], LEFT([name], CHARINDEX(' ', [name] + ' ')-1) as firstName,
SUBSTRING([name], charindex(' ', [name]+' ') + 1, LEN([name])) as lastName
from Author 
;

I am quite new to sql, any help is highly appreciated! 我对sql还是很陌生,非常感谢您的帮助! Thanks in advance! 提前致谢!

EDIT: for those who ever come across this need for help, this line helps: 编辑:对于那些遇到这种需要帮助的人,此行可以帮助:

select substr(t.string,instr(t.string,' ',-1)+1) last_word  

For Oracle DB try this : 对于Oracle DB,请尝试以下操作:

WITH t AS
  (SELECT name AS l FROM <your query>
  )
SELECT SUBSTR(l,instr(l,' ',-1)+1,LENGTH(l)) FROM t;

SUBSTRING_INDEX()在这种情况下应该可以工作。

select name, SUBSTRING_INDEX(name,' ',-1) as lastName from Author;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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