Home » SQL & PL/SQL » SQL & PL/SQL » specific text from the string (oracle)
|
|
|
|
Re: specific text from the string [message #689927 is a reply to message #689926] |
Tue, 30 July 2024 12:51   |
Solomon Yakobson
Messages: 3305 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   |
Solomon Yakobson
Messages: 3305 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.
|
|
|
|
|
Goto Forum:
Current Time: Sat May 03 21:21:16 CDT 2025
|