【问题标题】:Spacing reading from a String in SQL从 SQL 中读取字符串的间距
【发布时间】:2012-01-16 04:04:16
【问题描述】:

我有一个字符串(带空格),我想从中分隔最后一个单词。例如:

"Steve Jobs" => Jobs
"Stack OverFlow Question" => Questions

PL/SQL 或 SQL 中是否有任何函数,以便我能够得到 JobsQuestion 分离出来的结果?

【问题讨论】:

  • 类似于stackoverflow.com/questions/7241090/… 我可以为您提供解决方案,但不幸的是需要使用REVERSE,我认为这是非标准的。
  • @Sparky plsql 标签暗示 Oracle。
  • 谢谢,我不确定,因为一些旧版本的 Microsoft SQL 曾经被称为 PL/SQL...感谢您添加标签...

标签: sql oracle plsql plsqldeveloper


【解决方案1】:

您可以使用INSTRSUBSTRINSTR 告诉您特定角色的位置。为起始位置传递 -1 告诉 Oracle 开始从字符串的末尾向后看字符串的前面。

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select 'Steve Jobs' str from dual union all
  3    select 'Stack Overflow Question' from dual
  4  )
  5  select substr( str, instr( str, ' ', -1 ) + 1 ) last_word
  6*   from x
SQL> /

LAST_WORD
----------------------------------------------------------------------  
Jobs
Question

【讨论】:

  • 伟大的先生,正是我想要的
【解决方案2】:

这是一种在 Microsoft SQL 中执行此操作的方法。概念应该与对 Oracle 语法的字符串函数更改相同。

declare @fldName varchar(30)
set @fldName = 'Steve Jobs'

select reverse(substring(reverse(@fldName),1,charindex(' ',reverse(@fldName))-1))

set @fldName = 'Stack Overflow Question'

select reverse(substring(reverse(@fldName),1,charindex(' ',reverse(@fldName))-1))

【讨论】:

  • 贾斯汀的答案是一个更好的解决方案,不确定 Oracle 是否支持 -1 起始位置。另外,它更容易阅读
猜你喜欢
  • 2016-02-11
  • 2020-07-02
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多