如何在 Oracle SQL 中选择直到特定字符的子字符串?
假设我有一个表列,其结果如下:
ABC_blahblahblah
DEFGH_moreblahblahblah
IJKLMNOP_moremoremoremore
我希望能够编写一个查询,从所述表中选择此列,但仅返回到下划线 (_) 字符的子字符串。例如:
ABC
DEFGH
IJKLMNOP
SUBSTRING 函数似乎无法胜任这项任务,因为它是基于位置的,并且下划线的位置各不相同。
我考虑了 TRIM 函数(特别是 RTRIM 函数):
SELECT RTRIM('listofchars' FROM somecolumn)
FROM sometable
但我不确定如何让它工作,因为它似乎只删除特定的字符列表/集,而且我实际上只在字符开头直到下划线字符。
Say I have a table column that has results like:
ABC_blahblahblah
DEFGH_moreblahblahblah
IJKLMNOP_moremoremoremore
I would like to be able to write a query that selects this column from said table, but only returns the substring up to the Underscore (_) character. For example:
ABC
DEFGH
IJKLMNOP
The SUBSTRING function doesn't seem to be up to the task because it is position-based and the position of the underscore varies.
I thought about the TRIM function (the RTRIM function specifically):
SELECT RTRIM('listofchars' FROM somecolumn)
FROM sometable
But I'm not sure how I'd get this to work since it only seems to remove a certain list/set of characters and I'm really only after the characters leading up to the Underscore character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 SUBSTR、INSTR 和 NVL(对于没有下划线的字符串)的组合将返回您想要的结果:
结果:
使用:
参考:
附录
如果使用 Oracle10g+,您可以通过 REGEXP_SUBSTR< 使用正则表达式/a>.
Using a combination of SUBSTR, INSTR, and NVL (for strings without an underscore) will return what you want:
Result:
Use:
Reference:
Addendum
If using Oracle10g+, you can use regex via REGEXP_SUBSTR.
这可以使用REGEXP_SUBSTR轻松完成。
请使用
其中 STRING_EXAMPLE 是您的字符串。
尝试:
它将解决您的问题。
This can be done using REGEXP_SUBSTR easily.
Please use
where STRING_EXAMPLE is your string.
Try:
It will solve your problem.
是正确的答案,如 user1717270 发布的
如果您使用
INSTR
,它会给您一个字符串的位置,假设它包含“_”。如果没有怎么办?答案是 0。因此,当你想打印字符串时,它会打印一个NULL
。示例:如果您想从“host.domain”中删除域。在某些情况下,您只有简称,即“主机”。您很可能想打印“host”。好吧,使用
INSTR
,它会给你一个NULL
,因为它没有找到任何“.”,即它将从0打印到0。使用REGEXP_SUBSTR
code> 在所有情况下你都会得到正确的答案:HOST
和
HOST
is the right answer, as posted by user1717270
If you use
INSTR
, it will give you the position for a string that assumes it contains "_" in it. What if it doesn't? Well the answer will be 0. Therefore, when you want to print the string, it will print aNULL
.Example: If you want to remove the domain from a "host.domain". In some cases you will only have the short name, i.e. "host". Most likely you would like to print "host". Well, with
INSTR
it will give you aNULL
because it did not find any ".", i.e. it will print from 0 to 0. WithREGEXP_SUBSTR
you will get the right answer in all cases:HOST
and
HOST
您需要获取第一个下划线的位置(使用 INSTR),然后使用 substr 获取从第一个字符到 (pos-1) 的字符串部分。
说明文档
Susbtr 文档
You need to get the position of the first underscore (using INSTR) and then get the part of the string from 1st charecter to (pos-1) using substr.
Instr documentation
Susbtr Documentation
另一种可能性是使用 REGEXP_SUBSTR。
Another possibility would be the use of REGEXP_SUBSTR.
如果字符串位置不固定,那么通过下面的 Select 语句我们可以获得预期的输出。
数据
- --
要求- 在
CLIENT
列中搜索ClientId
字符串并返回相应的值。就像来自"clientId":"con-bjp" --> con-bjp(预期输出)
--
In case if String position is not fixed then by below Select statement we can get the expected output.
Data-
--
Requirement - Search
ClientId
string inCLIENT
column and return the corresponding value. Like From"clientId":"con-bjp" --> con-bjp(Expected output)
--
如果列中的所有字符串都没有下划线,请记住这一点
(...否则如果输出为空值):
Remember this if all your Strings in the column do not have an underscore
(...or else if null value will be the output):
要从大字符串中查找任何子字符串:
然后要从
String_value
中查找字符串'Ple'
,我们可以这样做:您将找到结果:
Ple
To find any sub-string from large string:
Then to find the string
'Ple'
fromString_value
we can do as:You will find result:
Ple