Home » SQL & PL/SQL » SQL & PL/SQL » specific text from the string (oracle)
specific text from the string [message #689923] Tue, 30 July 2024 08:40 Go to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Hi All,

How to get the text between two characters, are there any function in SQL.
I have tried with INSTR, SUBSTR, REGEXP_SUBSTR but didn't get the expected result.

Example:

Input Text: 56/971/267/80

I want the text between 1st slash (/) and second slash (/), i.e output should be 971.

Please help me with the SQL.

Thank you.

Regards
Suji
Re: specific text from the string [message #689924 is a reply to message #689923] Tue, 30 July 2024 09:10 Go to previous messageGo to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
I tried this one, I got the result as 200. Which is expected.  
SELECT
  rtrim(lTRIM(REGEXP_SUBSTR('14/200/60/00', '/[^/]+/'),'/'),'/') "REGEXPR_SUBSTR"
  FROM DUAL;
but are there any other best function to extract specific digits.

Thank you.

Regards
Sui
Re: specific text from the string [message #689925 is a reply to message #689924] Tue, 30 July 2024 12:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

SQL> select trim(both '/' from regexp_substr('56/971/267/80','/[0-9]+/')) from dual;
TRI
---
971

1 row selected.

SQL> select regexp_substr('56/971/267/80','([^/]+)',1,2) from dual;
REG
---
971

1 row selected.

SQL> select regexp_replace('56/971/267/80','^[^/]+/([^/]+)/.*$','\1') from dual;
REG
---
971

1 row selected.
Re: specific text from the string [message #689926 is a reply to message #689925] Tue, 30 July 2024 12:39 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3301
Registered: January 2010
Location: Connecticut, USA
Senior Member
OP never said string will always have two (or more) slashes, so be careful with regexp_replace:

SQL> select regexp_replace('56/57','^[^/]+/([^/]+)/.*$','\1') from dual
  2  /

REGEX
-----
56/57

SQL> select regexp_replace('56','^[^/]+/([^/]+)/.*$','\1') from dual
  2  /

RE
--
56

SQL>

SY.
Re: specific text from the string [message #689927 is a reply to message #689926] Tue, 30 July 2024 12:51 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3301
Registered: January 2010
Location: Connecticut, USA
Senior Member
And SUBSTR might be more efficient:

with strings as (
                 select '56/971/267/80' str from dual union all
                 select '56/971' str from dual union all
                 select '56' str from dual
               )
select  str,
        substr(
               str,
               instr(str,'/') + 1,
               instr(str,'/',1,2) - instr(str,'/') - 1
              ) sub_str
  from  strings
/

STR           SUB_STR
------------- ----------
56/971/267/80 971
56/971
56

SQL>
SY.
Re: specific text from the string [message #689928 is a reply to message #689927] Tue, 30 July 2024 13:20 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3301
Registered: January 2010
Location: Connecticut, USA
Senior Member
Actually, there is a caveat - NULL doesn't tell us if string doesn't have at least two slashes or first and second slashes are next to each other:

with strings as (
                 select '56/971/267/80' str from dual union all
                 select '56/971' str from dual union all
                 select '56//971' str from dual union all
                 select '56' str from dual
               )
select  str,
        substr(
               str,
               instr(str,'/') + 1,
               instr(str,'/',1,2) - instr(str,'/') - 1
              ) sub_str
  from  strings
/

STR           SUB_STR
------------- ----------
56/971/267/80 971
56/971
56//971
56

SQL>
If we care about such distinction then we can use something like:

with strings as (
                 select '56/971/267/80' str from dual union all
                 select '56/971' str from dual union all
                 select '56//971' str from dual union all
                 select '56' str from dual
               )
select  str,
        substr(
               str,
               instr(str,'/') + 1,
               instr(str,'/',1,2) - instr(str,'/') - 1
              ) sub_str,
        case instr(str,'/',1,2)
          when 0 then 'Not Found'
          else 'Found'
        end flag
  from  strings
/

STR           SUB_STR    FLAG
------------- ---------- ---------
56/971/267/80 971        Found
56/971                   Not Found
56//971                  Found
56                       Not Found

SQL>
SY.
Re: specific text from the string [message #689936 is a reply to message #689928] Sun, 04 August 2024 12:48 Go to previous messageGo to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Thanks for your reply with the SQL query.
Re: specific text from the string [message #689937 is a reply to message #689925] Sun, 04 August 2024 12:51 Go to previous message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Thank you. These select statements suitable for my request.
Previous Topic: schedule is not running
Next Topic: diffrence between two chacter fields.
Goto Forum:
  


Current Time: Sat Nov 23 04:00:21 CST 2024