Remove characters between delimiter in Oracle - sql

I'm using Oracle database 18 c and Oracle Apex 22.
I have a string like this one:
'Hello this Marc ||www.stackoverflow.com|| welcome to our family'
I want to remove any character within this delimiter ||
So the output should be:
'Hello this Marc welcome to our family'
Can anyone help with a regexp query that does that?

The way you put it, combination of substr and instr functions returns that result:
Sample data:
SQL> with test (col) as
2 (select 'Hello this Marc ||www.stackoverflow.com|| welcome to our family' from dual)
Query:
3 select substr(col, 1, instr(col, '|') - 2) ||
4 substr(col, instr(col, '|', 1, 4) + 1) as result
5 from test;
RESULT
-------------------------------------
Hello this Marc welcome to our family
SQL>
Or, if you prefer regular expressions, remove everything between outmost pipe signs:
3 select regexp_replace(col, ' \|.+\|') result
4 from test;
RESULT
-------------------------------------
Hello this Marc welcome to our family
SQL>

Related

shorten the string to the desired values

I have a table with a "Link" attribute. It has the following meaning:
INC102
INC1020
INC10200
I want to get the following result:
INC102
INC1020
INC10200
I need to leave the INC and the numbers after it without .
Tell me which command will help here? Since I understand that "Substr" will not work here.
I am use SQL Developer - Oracle
If you just want to extract "INC" with the following digits, use regexp_substr():
select regexp_substr(link, 'INC[0-9]+')
Here is a db<>fiddle.
Since I understand that "Substr" will not work here.
Says who?
SQL> with test (col) as
2 (select 'INC102' from dual union all
3 select 'INC1020' from dual union all
4 select 'INC10200' from dual
5 )
6 select
7 substr(col,
8 instr(col, '>') + 1,
9 instr(col, '<', instr(col, '>')) - instr(col, '>') - 1
10 ) result
11 from test;
RESULT
------------------------------------------------------------------------------------------------------------------------
INC102
INC1020
INC10200
SQL>
What does it do?
lines #1 - 5: sample data
line #8: starting point of the SUBSTR function is one character after the first > sign
line #9: length (used as the 3rd parameter of the SUBSTR) is position of the first < that follows the first > minus position of the first >
And that's it ... why wouldn't it work?
You could treat [<>] as the word delimiter and take the second word:
with test (col) as
( select 'INC102' from dual union all
select 'INC1020' from dual union all
select 'INC10200' from dual
)
select regexp_substr(col,'[^<>]+', 1, 2)
from test;
REGEXP_SUBSTR(COL,'[^<>]+',1,2)
-------------------------------
INC102
INC1020
INC10200

How to get first string after character Oracle SQL

I'm trying to get first string after a character.
Example is like
ABCDEF||GHJ||WERT
I need only
GHJ
I tried to use REGEXP but i couldnt do it.
Can anyone help me with please?
Thank you
Somewhat simpler:
SQL> select regexp_substr('ABCDEF||GHJ||WERT', '\w+', 1, 2) result from dual;
^
RES |
--- give me the 2nd "word"
GHJ
SQL>
which reads as: give me the 2nd word out of that string. Won't work properly if GHJ consists of several words (but that's not what your example suggests).
Something like I interpret with a separator in place, In this case it is || or | example is with oracle database
-- pattern -- > [^] represents non-matching character and + for says one or more character followed by ||
-- 3rd parameter --> starting position
-- 4th parameter --> nth occurrence
WITH tbl(str) AS
(SELECT 'ABCDEF||GHJ||WERT' str FROM dual)
SELECT regexp_substr(str
,'[^||]+'
,1
,2) output
FROM tbl;
I think the most general solution is:
WITH tbl(str) AS (
SELECT 'ABCDEF||GHJ||WERT' str FROM dual UNION ALL
SELECT 'ABC|DEF||GHJ||WERT' str FROM dual UNION ALL
SELECT 'ABClDEF||GHJ||WERT' str FROM dual
)
SELECT regexp_replace(str, '^.*\|\|(.*)\|\|.*', '\1')
FROM tbl;
Note that this works even if the individual elements contain punctuation or a single vertical bar -- which the other solutions do not. Here is a comparison.
Presumably, the double vertical bar is being used for maximum flexibility.
You should use regexp_substr function
select regexp_substr('ABCDEF||GHJ||WERT ', '\|{2}([^|]+)', 1, 1, 'i', 1) str
from dual;
STR
---
GHJ

Need to remove the exact group of characters

I need to remove all the characters after a particular string (-->).
select
REGEXP_SUBSTR('-->Team Name - Red-->blue', '[^(-->)]+')
from dual;
expected result from the above query is "Team Name - Red". But its returning "Team Name".
Its filtering out everything whenever it matches any of one character.
You can still use Regexp_Substr() analytic function :
Select Regexp_Substr('-->Team Name - Red-->blue',
'-{2}>(.*?)-{2}>',1,1,null,1) as "Result"
From dual;
Result
---------------
Team Name - Red
-{2}> ~ exactly twice occurence of - and single occurence of > e.g. ( --> )
(.*?) ~ matches anything delimited by the pattern above
Demo
You could try using REGEXP_REPLACE here with a capture group:
SELECT
REGEXP_REPLACE('-->Team Name - Red-->blue', '.*-->(.*?)-->.*', '\1')
FROM dual;
The output from this is Team Name - Red
Demo
It seems that you, actually, want to return string between two --> marks. A good, old substr + instr option would be
SQL> with test (col) as
2 (select '-->Team Name - Red-->blue' from dual)
3 select substr(col,
4 instr(col, '-->', 1, 1) + 3,
5 instr(col, '-->', 1, 2) - instr(col, '-->', 1, 1) - 3
6 ) result
7 from test;
RESULT
---------------
Team Name - Red
SQL>

Oracle Regexp_substr String

I have String like a123bcd-e2343fg-hij-dfgh
and I want OUTPUT e2343fg-hij-dfgh using Regular_expression in oracle.
select regexp_substr('abcd-efg-hij','-[^-]+'1) from dual;
You may apply regexp_substr with [^-]+[-^] pattern and then ltrim as :
select ltrim('a123bcd-e2343fg-hij-dfgh',
regexp_substr('a123bcd-e2343fg-hij-dfgh','[^-]+[-^]')) as output_string
from dual;
or better to call with bind variable :
select ltrim('&str', regexp_substr('&str','[^-]+[-^]')) as output_string
from dual;
where &str may be replaced with a123bcd-e2343fg-hij-dfgh after prompted.
Rextester Demo
Why regular expression, when a trivial SUBSTR + INSTR does the job nicely & quickly? True, it will look smarter, but I can't see any other benefit.
SQL> with test (col) as
2 (select 'a123bcd-e2343fg-hij-dfgh' from dual)
3 select substr(col, instr(col, '-') + 1) result
4 from test;
RESULT
----------------
e2343fg-hij-dfgh
SQL>
select substr('abcd-efg-hij',
regexp_instr('abcd-efg-hij','-[^-]+')+1,length('abcd-efg-hij'))
from dual;
try this
For the sake of argument, regexp_replace works too. This regex matches anything up to and including the first dash, and remembers the rest which it returns.
with tbl(str) as (
select 'a123bcd-e2343fg-hij-dfgh' from dual
)
select regexp_replace(str, '^.*?-(.*)', '\1')
from tbl;
Keep in mind if regexp_substr() does not find a match, it returns NULL but if regexp_replace() does not find a match it return the original string.

Oracle REGEXP_SUBSTR | Fetch string between two delimiters

I have a string Organization, INC..Truck/Equipment Failure |C. I want to fetch the sub-string after organization name (after two '..' characters) and before pipe character. So the output string should be - Truck/Equipment Failure.
Can you please help.
I have been trying forming regexp like this but doesn't seem working.
select regexp_substr('Organization, INC..Truck/Equipment Failure |C', '[^.]+',1,2) from dual;
You may use this.
SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure |C',
'([^.]+)\|',
1,
1,
NULL,
1)
FROM DUAL;
EDIT: This will match exactly two dots followed by one or more characters other than a | till the end of string.
SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure',
'\.{2}([^|]+)',
1,
1,
NULL,
1)
FROM DUAL;
DEMO
Classic SUBSTR + INSTR option:
SQL> with test as (select 'Organization, INC..Truck/Equipment Failure |C' col from dual)
2 select substr(col, instr(col, '..') + 2,
3 instr(col, '|') - instr(col, '..') - 2
4 ) result
5 from test;
RESULT
------------------------
Truck/Equipment Failure
SQL>