最后一个字在一个句子:在SQL(普通前pressions可能吗?) [英] Last word in a sentence: In SQL (regular expressions possible?)
问题描述
我需要这在Oracle SQL(10gR2中)来完成。但我想,我宁愿说白了,有什么好的,高效的算法是好的。
由于线路(或句子,包含一个或多个单词,英语),你将如何找到句子的最后一个字?
下面是我曾尝试在SQL。但是,我希望看到这样的一条有效途径。
选择反向(SUBSTR(反向(安培; p_word_in)
,0
,INSTR(反向(安培; p_word_in),'')
)
)
从双;
当时的想法是扭转字符串,找到第一个出现的空间,检索子和反向字符串。它是相当有效率?是一个普通的EX pression可用?我在Oracle 10g的R2。但我不介意看到在其他编程语言的任何企图,我不会头脑写一个PL / SQL函数,如果需要的话。
更新:
杰弗里·肯普给予了精彩的回答。这完美的作品。
答案
SELECT SUBSTR(安培;一句话,INSTR(安培;一句话,'',-1)+ 1)
从双
我觉得它更简单与INSTR / SUBSTR:
带Q AS(SELECT'ABC DEF GHIAS句话从DUAL)
SELECT SUBSTR(句子,INSTR(一句话,'',-1)+ 1)
从q;
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?
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;
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.
Update:
Jeffery Kemp has given a wonderful answer. This works perfectly.
Answer
SELECT SUBSTR(&sentence, INSTR(&sentence,' ',-1) + 1)
FROM dual
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;
这篇关于最后一个字在一个句子:在SQL(普通前pressions可能吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!