SQL 如何在字符oracle之后获取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45165587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:16:49  来源:igfitidea点击:

How to get string after character oracle

sqloraclesubstring

提问by Aruna Raghunam

I have VP3 - Art & Design and HS5 - Health & Social Care, I need to get string after '-'in Oracle. Can this be achieved using substring?

我有VP3 - Art & Design and HS5 - Health & Social Care,我需要'-'在 Oracle 中获取字符串。这可以使用子字符串来实现吗?

回答by Tim Biegeleisen

For a string operation as simple as this, I might just use the base INSTR()and SUBSTR()functions. In the query below, we take the substring of your column beginning at two positions afterthe hyphen.

对于像这样简单的字符串操作,我可能只使用基数INSTR()SUBSTR()函数。在下面的查询中,我们从连字符的两个位置开始获取列的子字符串。

SELECT
    SUBSTR(col, INSTR(col, '-') + 2) AS subject
FROM yourTable

We could also use REGEXP_SUBSTR()here (see Gordon's answer), but it would be a bit more complex and the performance might not be as good as the above query.

我们也可以REGEXP_SUBSTR()在这里使用(参见 Gordon 的回答),但它会更复杂一些,而且性能可能不如上面的查询。

回答by Gordon Linoff

You can use regexp_substr():

您可以使用regexp_substr()

select regexp_substr(col, '[^-]+', 1, 2)

If you want to remove an optional space, then you can use trim():

如果要删除可选空格,则可以使用trim()

select trim(leading ' ', regexp_substr(col, '[^-]+', 1, 2))

The non-ovious parameters mean

非显而易见的参数意味着

  • 1-- search from the first character of the source. 1is the default, but you have to set it anyway to be able to provide the second parameter.
  • 2-- take the second match as the result substring. the default would be 1.
  • 1-- 从源的第一个字符开始搜索。1是默认值,但无论如何您都必须设置它才能提供第二个参数。
  • 2-- 将第二个匹配项作为结果子串。默认值为 1。