Oracle SQL: Using CASE in a SELECT statement with Substr - sql

In a SELECT statement would it be possible to evaluate a Substr using CASE? Or what would be the best way to return a sub string based on a condition?
I am trying to retrieve a name from an event description column of a table. The string in the event description column is formatted either like text text (Mike Smith) text text or text text (Joe Schmit (Manager)) text text. I would like to return the name only, but having some of the names followed by (Manager) is throwing off my SELECT statement.
This is my SELECT statement:
SELECT *
FROM (
SELECT Substr(Substr(eventdes,Instr(eventdes,'(')+1),1,
Instr(eventdes,')') - Instr(eventdes,'(')-1)
FROM mytable
WHERE admintype = 'admin'
AND entrytime BETWEEN sysdate - (5/(24*60)) AND sysdate
AND eventdes LIKE '%action taken by%'
ORDER BY id DESC
)
WHERE ROWNUM <=1
This returns things like Mike Smith if there is no (Manager), but returns things like Joe Schmit (Manager if there is.
Any help would be greatly appreciated.

SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE MYTABLE ( id, admintype, entrytime, eventdes ) AS
SELECT 1, 'admin', SYSDATE, 'action taken by (John Doe (Manager)) more text' FROM DUAL;
Query 1:
SELECT *
FROM ( SELECT REGEXP_SUBSTR( eventdes, '\((.*?)(\s*\(.*?\))?\)', 1, 1, 'i', 1 )
FROM mytable
WHERE admintype = 'admin'
AND entrytime BETWEEN sysdate - (5/(24*60)) AND sysdate
AND eventdes LIKE '%action taken by%'
ORDER BY id DESC
)
WHERE ROWNUM <=1
Results:
| REGEXP_SUBSTR(EVENTDES,'\((.*?)(\S*\(.*?\))?\)',1,1,'I',1) |
|------------------------------------------------------------|
| John Doe |
Edit:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE MYTABLE ( id, admintype, entrytime, eventdes ) AS
SELECT 1, 'admin', SYSDATE, 'action taken by (Doe, John (Manager)) more text' FROM DUAL;
Query 1:
SELECT SUBSTR( Name, INSTR( Name, ',' ) + 1 ) || ' ' || SUBSTR( Name, 1, INSTR( Name, ',' ) - 1 ) AS Full_Name,
REGEXP_REPLACE( Name, '^(.*?),\s*(.*)$', '\2 \1' ) AS Full_Name2
FROM ( SELECT REGEXP_SUBSTR( eventdes, '\((.*?)(\s*\(.*?\))?\)', 1, 1, 'i', 1 ) AS Name
FROM mytable
WHERE admintype = 'admin'
-- AND entrytime BETWEEN sysdate - (5/(24*60)) AND sysdate
AND eventdes LIKE '%action taken by%'
ORDER BY id DESC
)
WHERE ROWNUM <=1
Results:
| FULL_NAME | FULL_NAME2 |
|-----------|------------|
| John Doe | John Doe |

You could use INSTR to search the last ')', but I would prefer a Regular Expression.
This extracts everything between the first '(' and the last ')' and the TRIMs remove the brackets (Oracle doesn't support look around in RegEx):
RTRIM(LTRIM(REGEXP_SUBSTR(eventdes, '\(.*\)'), '('), ')')

There is probably a regex that would do this without a replace but I am not smart enough to work it out so I combined a REGEXP_SUBSTRING that replaces your multiple instr methods and a REGEXP_REPLACE to replace the ' (MANAGER)' portion is it exists.
SELECT *
FROM (
SELECT REGEXP_REPLACE(
REGEXP_SUBSTR(eventdes, '\((.*)\)', 1, 1, 'i', 1), ' \(MANAGER\)', '')
FROM mytable
WHERE admintype = 'admin'
AND entrytime BETWEEN sysdate - (5/(24*60)) AND sysdate
AND eventdes LIKE '%action taken by%'
ORDER BY id DESC
)
WHERE ROWNUM <=1

Here's a slightly different take on the problem. This regexp starts at the position of the first open paren + 1 (the first letter of the name), then returns the first group from that point in the string on that consists of 1 or more characters that is not a close paren or an open paren. The TRIM gets rid of the trailing space if there is a manager.
SQL> with tbl(id, admintype, entrytime, eventdes ) AS
2 ( SELECT 1, 'admin', SYSDATE, 'action taken by (John Doe (Manager)) more text' FROM DUAL
3 union
4 SELECT 1, 'admin', SYSDATE, 'action taken by (Jane Doe) more text' FROM DUAL
5 )
6 select trim(regexp_substr(eventdes, '([^\)\(]+)', instr(eventdes, '(')+1, 1)) name
7 from tbl;
NAME
----------------------------------------------
Jane Doe
John Doe
SQL>

Related

Substring from underscore and onwards in Oracle

I have a string with under score and some characters. I need to apply substring and get values to the left excluding underscore. So I applied below formula and its working correctly for those strings which have underscore (_). But for strings without (_) it is bringing NULL. Any suggestions how this can be handled in the substring itself.
Ex: ABC_BASL ---> Works correctly; ABC ---> gives null
My formula as below -
select SUBSTR('ABC_BAS',1,INSTR('ABC_BAS','_')-1) from dual;
ABC
select SUBSTR('ABC',1,INSTR('ABC','_')-1) from dual;
(NULL)
You could use a CASE expression to first check for an underscore:
WITH yourTable AS (
SELECT 'ABC_BAS' AS col FROM dual UNION ALL
SELECT 'ABC' FROM dual
)
SELECT
CASE WHEN col LIKE '%\_%' ESCAPE '\'
THEN SUBSTR(col, 1, INSTR(col, '_') - 1)
ELSE col END AS col_out
FROM yourTable;
Use regular expression matching:
SELECT REGEXP_SUBSTR('ABC_BAS', '(.*)([_]|$)?', 1, 1, NULL, 1) FROM DUAL;
returns 'ABC', and
SELECT REGEXP_SUBSTR('ABC', '(.*)([_]|$)?', 1, 1, NULL, 1) FROM DUAL;
also returns 'ABC'.
db<>fiddle here
EDIT
The above gives correct results, but I missed the easiest possible regular expression to do the job:
SELECT REGEXP_SUBSTR('ABC_BAS', '[^_]*') FROM DUAL;
returns 'ABC', as does
SELECT REGEXP_SUBSTR('ABC', '[^_]*') FROM DUAL;
db<>fiddle here
Yet another approach is to use the DECODE in the length parameter of the substr as follows:
substr(str,
1,
decode(instr(str,'_'), 0, lenght(str), instr(str,'_') - 1)
)
You seem to want everything up to the first '_'. If so, one method usesregexp_replace():
select regexp_replace(str, '(^[^_]+)_.*$', '\1')
from (select 'ABC' as str from dual union all
select 'ABC_BAS' from dual
) s
A simpler method is:
select regexp_substr(str, '^[^_]+')
from (select 'ABC' as str from dual union all
select 'ABC_BAS' from dual
) s
Here is a db<>fiddle.
I'd use
regexp_replace(text,'_.*')
or if performance was a concern,
substr(text, 1, instr(text||'_', '_') -1)
For example,
with demo(text) as
( select column_value
from table(sys.dbms_debug_vc2coll('ABC', 'ABC_DEF', 'ABC_DEF_GHI')) )
select text
, regexp_replace(text,'_.*')
, substr(text, 1, instr(text||'_', '_') -1)
from demo;
TEXT REGEXP_REPLACE(TEXT,'_.*') SUBSTR(TEXT,1,INSTR(TEXT||'_','_')-1)
------------ --------------------------- -------------------------------------
ABC ABC ABC
ABC_DEF ABC ABC
ABC_DEF_GHI ABC ABC
Ok i think i got it. Add nvl to the substring and insert the condition as below -
select nvl(substr('ABC',1,instr('F4001Z','_')-1),'ABC') from dual;

distinct and sum if like

I have a table as the following
name
-----------
1#apple#1
2#apple#2
3#apple#4
4#box#4
5#box#5
and I want to get the result as:
name
--------------
apple 3
box 2
Thanks in advance for your help
This is what you need.
select
SUBSTRING(
name,
CHARINDEX('#', name) + 1,
LEN(name) - (
CHARINDEX('#', REVERSE(name)) + CHARINDEX('#', name)
)
),
count(1)
from
tbl
group by
SUBSTRING(
name,
CHARINDEX('#', name) + 1,
LEN(name) - (
CHARINDEX('#', REVERSE(name)) + CHARINDEX('#', name)
)
)
If your data does not contain any full stops (or periods depending on your vernacular), and the length of your string is less than 128 characters, then you can use PARSENAME to effectively split your string into parts, and extract the 2nd part:
DECLARE #T TABLE (Val VARCHAR(20));
INSERT #T (Val)
VALUES ('1#apple#1'), ('2#apple#2'), ('3#apple#4'),
('4#box#4'), ('5#box#5');
SELECT Val = PARSENAME(REPLACE(t.Val, '#', '.'), 2),
[Count] = COUNT(*)
FROM #T AS t
GROUP BY PARSENAME(REPLACE(t.Val, '#', '.'), 2);
Otherwise you will need to use CHARINDEX to find the first and last occurrence of # within your string (REVERSE is also needed to get the last position), then use SUBSTRING to extract the text between these positions:
DECLARE #T TABLE (Val VARCHAR(20));
INSERT #T (Val)
VALUES ('1#apple#1'), ('2#apple#2'), ('3#apple#4'),
('4#box#4'), ('5#box#5');
SELECT Val = SUBSTRING(t.Val, x.FirstPosition + 1, x.LastPosition - x.FirstPosition),
[Count] = COUNT(*)
FROM #T AS t
CROSS APPLY
( SELECT CHARINDEX('#', t.Val) ,
LEN(t.Val) - CHARINDEX('#', REVERSE(t.Val))
) AS x (FirstPosition, LastPosition)
GROUP BY SUBSTRING(t.Val, x.FirstPosition + 1, x.LastPosition - x.FirstPosition);
use case when
select case when name like '%apple%' then 'apple'
when name like '%box%' then 'box' end item_name,
count(*)
group by cas when name like '%apple%' then 'apple'
when name like '%box%' then 'box' end
No DBMS specified, so here is a postgres variant. The query does use regexps to simplify things a bit.
with t0 as (
select '1#apple#1' as value
union all select '2#apple#2'
union all select '3#apple#4'
union all select '4#box#4'
union all select '5#box#5'
),
trimmed as (
select regexp_replace(value,'[0-9]*#(.+?)#[0-9]*','\1') as name
from t0
)
select name, count(*)
from trimmed
group by name
order by name
DB Fiddle
Update
For Oracle DMBS, the query stays basically the same:
with t0 as (
select '1#apple#1' as value from dual
union all select '2#apple#2' from dual
union all select '3#apple#4' from dual
union all select '4#box#4' from dual
union all select '5#box#5' from dual
),
trimmed as (
select regexp_replace(value,'[0-9]*#(.+?)#[0-9]*','\1') as name
from t0
)
select name, count(*)
from trimmed
group by name
order by name
NAME | COUNT(*)
:---- | -------:
apple | 3
box | 2
db<>fiddle here
Update
MySQL 8.0
with t0 as (
select '1#apple#1' as value
union all select '2#apple#2'
union all select '3#apple#4'
union all select '4#box#4'
union all select '5#box#5'
),
trimmed as (
select regexp_replace(value,'[0-9]*#(.+?)#[0-9]*','$1') as name
from t0
)
select name, count(*)
from trimmed
group by name
order by name
name | count(*)
:---- | -------:
apple | 3
box | 2
db<>fiddle here
You can use case and group by to do the same.
select new_col , count(new_col)
from
(
select case when col_name like '%apple%' then 'apple'
when col_name like '%box%' then 'box'
else 'others' end new_col
from table_name
)
group by new_col
;

SQL to find upper case words from a column

I have a description column in my table and its values are:
This is a EXAMPLE
This is a TEST
This is a VALUE
I want to display only EXAMPLE, TEST, and VALUE from the description column.
How do I achieve this?
This could be a way:
-- a test case
with test(id, str) as (
select 1, 'This is a EXAMPLE' from dual union all
select 2, 'This is a TEST' from dual union all
select 3, 'This is a VALUE' from dual union all
select 4, 'This IS aN EXAMPLE' from dual
)
-- concatenate the resulting words
select id, listagg(str, ' ') within group (order by pos)
from (
-- tokenize the strings by using the space as a word separator
SELECT id,
trim(regexp_substr(str, '[^ ]+', 1, level)) str,
level as pos
FROM test t
CONNECT BY instr(str, ' ', 1, level - 1) > 0
and prior id = id
and prior sys_guid() is not null
)
-- only get the uppercase words
where regexp_like(str, '^[A-Z]+$')
group by id
The idea is to tokenize every string, then cut off the words that are not made by upper case characters and then concatenate the remaining words.
The result:
1 EXAMPLE
2 TEST
3 VALUE
4 IS EXAMPLE
If you need to handle some other character as an upper case letter, you may edit the where condition to filter for the matching words; for example, with '_':
with test(id, str) as (
select 1, 'This is a EXAMPLE' from dual union all
select 2, 'This is a TEST' from dual union all
select 3, 'This is a VALUE' from dual union all
select 4, 'This IS aN EXAMPLE' from dual union all
select 5, 'This IS AN_EXAMPLE' from dual
)
select id, listagg(str, ' ') within group (order by pos)
from (
SELECT id,
trim(regexp_substr(str, '[^ ]+', 1, level)) str,
level as pos
FROM test t
CONNECT BY instr(str, ' ', 1, level - 1) > 0
and prior id = id
and prior sys_guid() is not null
)
where regexp_like(str, '^[A-Z_]+$')
group by id
gives:
1 EXAMPLE
2 TEST
3 VALUE
4 IS EXAMPLE
5 IS AN_EXAMPLE
Here's another solution. It was inspired by Aleksej's answer.
The idea? Get all the words. Then aggregate only fully uppercased to a list.
Sample data:
create table descriptions (ID int, Description varchar2(100));
insert into descriptions (ID, Description)
select 1 as ID, 'foo Foo FOO bar Bar BAR' as Description from dual
union all select 2, 'This is an EXAMPLE TEST Description VALUE' from dual
;
Query:
select id, Description, listagg(word, ',') within group (order by pos) as UpperCaseWords
from (
select
id, Description,
trim(regexp_substr(Description, '\w+', 1, level)) as word,
level as pos
from descriptions t
connect by regexp_instr(Description, '\s+', 1, level - 1) > 0
and prior id = id
and prior sys_guid() is not null
)
where word = upper(word)
group by id, Description
Result:
ID | DESCRIPTION | UPPERCASEWORDS
-- | ----------------------------------------- | ------------------
1 | foo Foo FOO bar Bar BAR | FOO,BAR
2 | This is an EXAMPLE TEST Description VALUE | EXAMPLE,TEST,VALUE
It is possible to achieve this thanks to the REGEXP_REPLACE function:
SELECT REGEXP_REPLACE(my_column, '(^[A-Z]| |[a-z][A-Z]*|[A-Z]*[a-z])', '') AS Result FROM my_table
It uses a regex which replaces first upper case char of the line and converts every lower case char and space with blanks.
Try this:
SELECT SUBSTR(column_name, INSTR(column_name,' ',-1) + 1)
FROM your_table;
This should do the trick:
SELECT SUBSTR(REGEXP_REPLACE(' ' || REGEXP_REPLACE(description, '(^[A-Z]|[a-z]|[A-Z][a-z]+|[,])', ''), ' +', ' '), 2, 9999) AS only_upper
FROM (
select 'Hey IF you do not know IT, This IS a test of UPPERCASE and IT, with good WILL and faith, Should BE fine to be SHOWN' description
from dual
)
I have added condition to strip commas, you can add inside that brakets other special characters to remove.
ONLY_UPPER
-----------------------------------
IF IT IS UPPERCASE IT WILL BE SHOWN
This is a function based on some of the regular expression answers.
create or replace function capwords(orig_string varchar2)
return varchar2
as
out_string varchar2(80);
begin
out_string := REGEXP_REPLACE(orig_string, '([a-z][A-Z_]*|[A-Z_]*[a-z])', '');
out_string := REGEXP_REPLACE(trim(out_string), '( *)', ' ');
return out_string;
end;
/
Removes strings of upper case letters and underscores that have lower case letters
on either end. Replaces multiple adjacent spaces with one space.
Trims extra spaces off of the ends. Assumes max size of 80 characters.
Slightly edited output:
>select id,str,capwords(str) from test;
ID STR CAPWORDS(STR)
---------- ------------------------------ ------------------
1 This is a EXAMPLE EXAMPLE
2 This is a TEST TEST
3 This is a VALUE VALUE
4 This IS aN EXAMPLE IS EXAMPLE
5 This is WITH_UNDERSCORE WITH_UNDERSCORE
6 ThiS IS aN EXAMPLE IS EXAMPLE
7 thiS IS aN EXAMPLE IS EXAMPLE
8 This IS wiTH_UNDERSCORE IS
If you only need to "display" the result without changing the values in the column then you can use CASE WHEN (in the example Description is the column name):
Select CASE WHEN Description like '%EXAMPLE%' then 'EXAMPLE' WHEN Description like '%TEST%' then 'TEST' WHEN Description like '%VALUE%' then 'VALUE' END From [yourTable]
The conditions are not case sensitive even if you write it all in uppercase.
You can add Else '<Value if all conditions are wrong>' before the END in case there are descriptions that don't contain any of the values. The example will return NULL for those cases, and writing ELSE Description will return the original value of that row.
It also works if you need to update. It is simple and practical, easy way out, haha.

List functions to find differences between to delimited sets

I have two delimited lists as follows and need to find what has changed.
Example: ( here | is separator )
old string: Joe | Public | NY
new string: Joe | Smith | NY
Since only second member of list has changed, the output should show only what has changed as follows :
Output:
- | Smith | -
Are there any Oracle functions or standard packages available which can compare these two delimited strings/sets and determine what has changed ?
Storing delimited lists in strings is not a good idea; that's missing the point of relational databases (as #Tim pointed out in a comment).
If you are generating the delimited string in an earlier step then go back to the source data and work directly from that.
If you are really stuck with those strings, there are various methods to tokenise delimited strings into individual values (as rows in a result set). This uses regular expression to split both string at once into two columns - for this I'm using a bind variable for each string to void repeating the literals, but you might be getting your strings from table columns (which would need a bit more work) or PL/SQL variables or whatever:
var old_string varchar2(20);
var new_string varchar2(20);
exec :old_string := 'Joe|Public|NY';
exec :new_string := 'Joe|Smith|NY';
with vals (pos, old_val, new_val) as (
select level,
regexp_substr(:old_string, '(.*?)(\||$)', 1, level, null, 1),
regexp_substr(:new_string, '(.*?)(\||$)', 1, level, null, 1)
from dual
connect by level < greatest(regexp_count(:old_string, '(.*?)(\||$)'),
regexp_count(:new_string, '(.*?)(\||$)'))
)
select * from vals;
POS OLD_VAL NEW_VAL
---------- ---------- ----------
1 Joe Joe
2 Public Smith
3 NY NY
It's now simple to compare the two columns to see what has changed, and then (if you must) aggregate them back into a single string value:
with vals (pos, old_val, new_val) as (
select level,
regexp_substr(:old_string, '(.*?)(\||$)', 1, level, null, 1),
regexp_substr(:new_string, '(.*?)(\||$)', 1, level, null, 1)
from dual
connect by level < greatest(regexp_count(:old_string, '(.*?)(\||$)'),
regexp_count(:new_string, '(.*?)(\||$)'))
)
select listagg(case when (old_val is null and new_val is null)
or old_val = new_val then '-' else new_val end, '|')
within group (order by pos) as diff
from vals;
DIFF
--------------------
-|Smith|-
The case expression determines whether you see a dash, indicating no change, or the new value.
This should handle nulls (empty elements, i.e. two delimiters next to each other); it will also handle different numbers of elements, if that can happen:
exec :old_string := 'Joe|Public|NY||';
exec :new_string := 'Joe|Smith|NY||USA';
... same query ...
DIFF
--------------------
-|Smith|-|-|USA
But you should really fix your data model...
If the old and new strings are currently coming from two columns in a table, you could extend this to compare multiple rows; you just need to refer to a non-deterministic function in the connect by clause:
create table t42 (id, old_string, new_string) as
select 1, 'Joe|Public|NY', 'Joe|Smith|NY' from dual
union all select 2, 'Joe|Public|NY', 'Joe|Smith|NY|USA' from dual;
with vals (id, pos, old_val, new_val) as (
select id, level,
regexp_substr(old_string, '(.*?)(\||$)', 1, level, null, 1),
regexp_substr(new_string, '(.*?)(\||$)', 1, level, null, 1)
from t42
connect by id = prior id
and prior dbms_random.value is not null
and level < greatest(regexp_count(old_string, '(.*?)(\||$)'),
regexp_count(new_string, '(.*?)(\||$)'))
)
select id, listagg(case when (old_val is null and new_val is null)
or old_val = new_val then '-' else new_val end, '|')
within group (order by pos) as diff
from vals
group by id
order by id;
ID DIFF
---------- --------------------
1 -|Smith|-
2 -|Smith|-|USA
If they are coming from different rows or different tables then it's more complicated and #MTOs approach should be considered.
I should also point out that I assumed the spaces around your delimiter were to make the strings easier to read in the question; if they are actually in the data then the pattern can be adjusted (like #MTO's, again).
It's not a simple solution but you can do it in SQL using collections and a hierarchical query that uses regular expressions to match each element of the delimited list.
(Note: This method will work with multiple input rows.)
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( id, old_string, new_string ) AS
SELECT 1, 'Joe | Public | NY', 'Joe | Smith | NY' FROM DUAL UNION ALL
SELECT 2, 'Joe | Public | NY', 'Joe | Smith | NY|USA' FROM DUAL
/
CREATE TYPE indexed_string AS OBJECT(
idx INT,
value VARCHAR2(100)
)
/
CREATE TYPE indexed_string_table AS TABLE OF indexed_string
/
Query 1:
SELECT id,
( SELECT LISTAGG(
CASE
WHEN o.value = n.value THEN '-'
WHEN o.value IS NULL AND n.value IS NULL THEN '-'
ELSE n.value
END,
' | '
) WITHIN GROUP ( ORDER BY COALESCE( o.idx, n.idx ) )
FROM TABLE(
CAST(
MULTISET(
SELECT indexed_string(
LEVEL,
REGEXP_SUBSTR(
t.old_string,
'(.*?)($|\s*\|\s*)',
1,
LEVEL,
NULL,
1
)
)
FROM DUAL
CONNECT BY LEVEL < REGEXP_COUNT(
t.old_string,
'(.*?)($|\s*\|\s*)'
)
) AS indexed_string_table
)
) o
FULL OUTER JOIN
TABLE(
CAST(
MULTISET(
SELECT indexed_string(
LEVEL,
REGEXP_SUBSTR(
t.new_string,
'(.*?)($|\s*\|\s*)',
1,
LEVEL,
NULL,
1
)
)
FROM DUAL
CONNECT BY LEVEL < REGEXP_COUNT(
t.new_string,
'(.*?)($|\s*\|\s*)'
)
) AS indexed_string_table
)
) n
ON ( o.idx = n.idx )
) AS changes
FROM table_name t
Results:
| ID | CHANGES |
|----|---------------------|
| 1 | - | Smith | - |
| 2 | - | Smith | - | USA |

Reg Expression in oracle?

I have a string like this '102/103/104/106'
Now if i pass 102 as input then output should be the next field that is 103. if 103 then output should be 104 and if 106 then output should be null(as for last field I don't have any further expression). I can do this using procedure by splitting the string into arrays and comparing. But can I do this through sql statement something like this
select '102/103/104/106' from dual where [expression 102 or 103].
Thanks!!
You can do it in pure SQL with something like this:
--convert your string into rows
with vals as (
select
substr('102/103/104/106',
instr('102/103/104/106', '/', 1, level)-3,
3
) col,
level lvl
from dual
connect by level <= length('102/103/104/106')-length(replace('102/103/104/106', '/'))+1
)
select *
from (
select col,
lead(col) over (order by lvl) next_val -- find the next value in the list
from vals
)
where col = :val;
Basically, convert your string into rows by parsing it. Then use the analytic lead to find the "next" value.
-- p_whole_string = '102/103/104/106'
-- p_prev = '102'
select
regexp_substr(p_whole_string, '(^|/)' || p_prev || '/([^/]+)', 1, 1, null, 2)
as next
from dual;
Added NVL to return last value if 106 is entered:
SELECT NVL(REGEXP_SUBSTR('102/103/104/106', '(^|/)' || '106' || '/([^/]+)', 1, 1, null, 2), REGEXP_SUBSTR('102/103/104/106', '[^/]+$')) as nxt
FROM dual
/
works for Oracle form 10 up.
SELECT
REGEXP_SUBSTR(
REGEXP_SUBSTR('102/103/104/106', '(^|/)102/[^/]+'), -- returns 102/103
'[^/]+',1,2) val -- takes second part
FROM DUAL;
with parameters looks like this:
-- p_string_to_search = '102/103/104/106'
-- p_string_to_match = '102'
SELECT
REGEXP_SUBSTR(
REGEXP_SUBSTR(p_string_to_search, '(^|/)' || p_string_to_match ||'/[^/]+'), -- returns 102/103
'[^/]+',1,2) val -- takes second part
FROM DUAL;