一句话中的最后一个词:在 SQL 中(可能使用正则表达式?)
我需要在 Oracle SQL (10gR2) 中完成此操作.但我想,我宁愿说白了,任何好的、高效的算法都可以.
I need this to be done in Oracle SQL (10gR2). But I guess, I would rather put it plainly, any good, efficient algorithm is fine.
给定一行(或句子,包含一个或多个单词,英语),你将如何找到句子的最后一个单词?
Given a line (or sentence, containing one or many words, English), how will you find the last word of the sentence?
这是我在 SQL 中尝试过的.但是,我希望看到一种有效的方式来做到这一点.
Here is what I have tried in SQL. But, I would like to see an efficient way of doing this.
select reverse(substr(reverse(&p_word_in)
, 0
, instr(reverse(&p_word_in), ' ')
)
)
from dual;
这个想法是反转字符串,找到第一个出现的空格,检索子字符串并反转字符串.是不是很有效率?正则表达式是否可用?我在 Oracle 10g R2 上.但我不介意看到任何其他编程语言的尝试,如果需要,我不介意编写 PL/SQL 函数.
The idea was to reverse the string, find the first occurring space, retrieve the substring and reverse the string. Is it quite efficient? Is a regular expression available? I am on Oracle 10g R2. But I dont mind seeing any attempt in other programming language, I wont mind writing a PL/SQL function if need be.
Jeffery Kemp 给出了精彩的答案.这非常有效.
Jeffery Kemp has given a wonderful answer. This works perfectly.
SELECT SUBSTR(&sentence, INSTR(&sentence,' ',-1) + 1)
FROM dual
推荐答案
我认为使用 INSTR/SUBSTR 会更简单:
I reckon it's simpler with INSTR/SUBSTR:
WITH q AS (SELECT 'abc def ghi' AS sentence FROM DUAL)
SELECT SUBSTR(sentence, INSTR(sentence,' ',-1) + 1)
FROM q;
相关文章