最后一个字在一个句子:在SQL(普通前pressions可能吗?)一个字、句子、普通、SQL

2023-09-11 01:59:56 作者:关于心累了的,伤感女生累了

我需要这在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;

 
精彩推荐
图片推荐