如何在 Oracle SQL 中选择直到特定字符的子字符串?

发布于 2024-10-06 08:23:06 字数 451 浏览 7 评论 0原文

假设我有一个表列,其结果如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

撩心不撩汉 2024-10-13 08:23:06

使用 SUBSTR、INSTR 和 NVL(对于没有下划线的字符串)的组合将返回您想要的结果:

SELECT NVL(SUBSTR('ABC_blah', 0, INSTR('ABC_blah', '_')-1), 'ABC_blah') AS output
  FROM DUAL

结果:

output
------
ABC

使用:

SELECT NVL(SUBSTR(t.column, 0, INSTR(t.column, '_')-1), t.column) AS output
  FROM YOUR_TABLE t

参考:

附录

如果使用 Oracle10g+,您可以通过 REGEXP_SUBSTR< 使用正则表达式/a>.

Using a combination of SUBSTR, INSTR, and NVL (for strings without an underscore) will return what you want:

SELECT NVL(SUBSTR('ABC_blah', 0, INSTR('ABC_blah', '_')-1), 'ABC_blah') AS output
  FROM DUAL

Result:

output
------
ABC

Use:

SELECT NVL(SUBSTR(t.column, 0, INSTR(t.column, '_')-1), t.column) AS output
  FROM YOUR_TABLE t

Reference:

Addendum

If using Oracle10g+, you can use regex via REGEXP_SUBSTR.

零崎曲识 2024-10-13 08:23:06

这可以使用REGEXP_SUBSTR轻松完成。

请使用

REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) 

其中 STRING_EXAMPLE 是您的字符串。

尝试:

SELECT 
REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) 
from dual

它将解决您的问题。

This can be done using REGEXP_SUBSTR easily.

Please use

REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) 

where STRING_EXAMPLE is your string.

Try:

SELECT 
REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) 
from dual

It will solve your problem.

一腔孤↑勇 2024-10-13 08:23:06
SELECT REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1)  from dual

是正确的答案,如 user1717270 发布的

如果您使用 INSTR,它会给您一个字符串的位置,假设它包含“_”。如果没有怎么办?答案是 0。因此,当你想打印字符串时,它会打印一个 NULL
示例:如果您想从“host.domain”中删除域。在某些情况下,您只有简称,即“主机”。您很可能想打印“host”。好吧,使用INSTR,它会给你一个NULL,因为它没有找到任何“.”,即它将从0打印到0。使用REGEXP_SUBSTR code> 在所有情况下你都会得到正确的答案:

SELECT REGEXP_SUBSTR('HOST.DOMAIN','[^.]+',1,1)  from dual;

HOST

SELECT REGEXP_SUBSTR('HOST','[^.]+',1,1)  from dual;

HOST

SELECT REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1)  from dual

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 a NULL.
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 a NULL because it did not find any ".", i.e. it will print from 0 to 0. With REGEXP_SUBSTR you will get the right answer in all cases:

SELECT REGEXP_SUBSTR('HOST.DOMAIN','[^.]+',1,1)  from dual;

HOST

and

SELECT REGEXP_SUBSTR('HOST','[^.]+',1,1)  from dual;

HOST

青丝拂面 2024-10-13 08:23:06

您需要获取第一个下划线的位置(使用 INSTR),然后使用 substr 获取从第一个字符到 (pos-1) 的字符串部分。

  1  select 'ABC_blahblahblah' test_string,
  2         instr('ABC_blahblahblah','_',1,1) position_underscore,
  3         substr('ABC_blahblahblah',1,instr('ABC_blahblahblah','_',1,1)-1) result
  4*   from dual
SQL> /

TEST_STRING      POSITION_UNDERSCORE RES
---------------- ------------------  ---
ABC_blahblahblah                  4  ABC

说明文档

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.

  1  select 'ABC_blahblahblah' test_string,
  2         instr('ABC_blahblahblah','_',1,1) position_underscore,
  3         substr('ABC_blahblahblah',1,instr('ABC_blahblahblah','_',1,1)-1) result
  4*   from dual
SQL> /

TEST_STRING      POSITION_UNDERSCORE RES
---------------- ------------------  ---
ABC_blahblahblah                  4  ABC

Instr documentation

Susbtr Documentation

时光匆匆的小流年 2024-10-13 08:23:06

另一种可能性是使用 REGEXP_SUBSTR。

Another possibility would be the use of REGEXP_SUBSTR.

垂暮老矣 2024-10-13 08:23:06

如果字符串位置不固定,那么通过下面的 Select 语句我们可以获得预期的输出。

Table      Structure
ID         VARCHAR2(100 BYTE)
CLIENT     VARCHAR2(4000 BYTE)

数据

ID    CLIENT      
1001  {"clientId":"con-bjp","clientName":"ABC","providerId":"SBS"}  
1002 

- --

{"IdType":"AccountNo","Id":"XXXXXXXX3521","ToPricingId":"XXXXXXXX3521","clientId":"Test-Cust","clientName":"MFX"}

要求- 在CLIENT列中搜索ClientId字符串并返回相应的值。就像来自 "clientId":"con-bjp" --> con-bjp(预期输出)

select CLIENT,substr(substr(CLIENT,instr(CLIENT,'"clientId":"')+length('"clientId":"')),1,instr(substr(CLIENT,instr(CLIENT,'"clientId":"')+length('"clientId":"')),'"',1 )-1) cut_str from TEST_SC;

--

CLIENT                                                        cut_str 
-----------------------------------------------------------   ----------
{"clientId":"con-bjp","clientName":"ABC","providerId":"SBS"}    con-bjp
{"IdType":"AccountNo","Id":"XXXXXXXX3521","ToPricingId":"XXXXXXXX3521","clientId":"Test-Cust","clientName":"MFX"}   Test-Cust

In case if String position is not fixed then by below Select statement we can get the expected output.

Table      Structure
ID         VARCHAR2(100 BYTE)
CLIENT     VARCHAR2(4000 BYTE)

Data-

ID    CLIENT      
1001  {"clientId":"con-bjp","clientName":"ABC","providerId":"SBS"}  
1002 

--

{"IdType":"AccountNo","Id":"XXXXXXXX3521","ToPricingId":"XXXXXXXX3521","clientId":"Test-Cust","clientName":"MFX"}

Requirement - Search ClientId string in CLIENT column and return the corresponding value. Like From "clientId":"con-bjp" --> con-bjp(Expected output)

select CLIENT,substr(substr(CLIENT,instr(CLIENT,'"clientId":"')+length('"clientId":"')),1,instr(substr(CLIENT,instr(CLIENT,'"clientId":"')+length('"clientId":"')),'"',1 )-1) cut_str from TEST_SC;

--

CLIENT                                                        cut_str 
-----------------------------------------------------------   ----------
{"clientId":"con-bjp","clientName":"ABC","providerId":"SBS"}    con-bjp
{"IdType":"AccountNo","Id":"XXXXXXXX3521","ToPricingId":"XXXXXXXX3521","clientId":"Test-Cust","clientName":"MFX"}   Test-Cust
梦罢 2024-10-13 08:23:06

如果列中的所有字符串都没有下划线,请记住这一点
(...否则如果输出为空值):

SELECT COALESCE
(SUBSTR("STRING_COLUMN" , 0, INSTR("STRING_COLUMN", '_')-1), 
"STRING_COLUMN") 
AS OUTPUT FROM DUAL

Remember this if all your Strings in the column do not have an underscore
(...or else if null value will be the output):

SELECT COALESCE
(SUBSTR("STRING_COLUMN" , 0, INSTR("STRING_COLUMN", '_')-1), 
"STRING_COLUMN") 
AS OUTPUT FROM DUAL
救赎№ 2024-10-13 08:23:06

要从大字符串中查找任何子字符串:

string_value:=('This is String,Please search string 'Ple');

然后要从 String_value 中查找字符串 'Ple',我们可以这样做:

select substr(string_value,instr(string_value,'Ple'),length('Ple')) from dual;

您将找到结果:Ple

To find any sub-string from large string:

string_value:=('This is String,Please search string 'Ple');

Then to find the string 'Ple' from String_value we can do as:

select substr(string_value,instr(string_value,'Ple'),length('Ple')) from dual;

You will find result: Ple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文