How to remove all characters except 'E' in oracle - sql

I have strig like 'AA_0331L_02317_R5_P' and i want remove all characters except 'E' from second part after splitting with _ character, so here it is 0331N should become 0331 and if it comes like 0331E , then it should become 0331E .ie simply if i have i string like AA_0331N_02317_R5_P , then i want to be AA_0331_02317_R5_P and if i have a AA_0331E_02317_R5_P ,then it should be AA_0331E_02317_R5_P. I did like as shown below without any luck
SELECT REGEXP_REPLACE(REGEXP_SUBSTR( 'AA_0331L_02317_R5_P' , '[^_]+', 1, 2 ), '[^0-9]', '')
FROM dual

You might try something like the following -- keeping in mind that REGEXP_REPLACE() will return the original string if nothing is actually replaced. Here I'm using backreferences (if Oracle regexes had lookahead I could have omitted the 2nd capturing group and backreference):
WITH mytable AS (
SELECT 'AA_0331L_02317_R5_P' AS myvalue
FROM dual
UNION ALL
SELECT 'AA_0331N_02317_R5_P'
FROM dual
UNION ALL
SELECT 'AA_0331E_02317_R5_P'
FROM dual
)
SELECT myvalue
, REGEXP_REPLACE(myvalue, '^([^_]+_[^_]+)[^E](_)', '\1\2') mynewvalue
FROM mytable;
MYVALUE MYNEWVALUE
------------------------- -------------------------
AA_0331L_02317_R5_P AA_0331_02317_R5_P
AA_0331N_02317_R5_P AA_0331_02317_R5_P
AA_0331E_02317_R5_P AA_0331E_02317_R5_P

with s as (
select 'AA_0331L_02317_R5_P' str from dual union all
select 'AA_0331E_02317_R5_P' str from dual)
select str,
regexp_replace(regexp_substr(str, '[^_]+_[^_]+'), '[^E]$') || regexp_replace(str, '[^_]+_[^_]+', '', 1, 1) new_str
from s;
STR NEW_STR
------------------------------ ------------------------------
AA_0331L_02317_R5_P AA_0331_02317_R5_P
AA_0331E_02317_R5_P AA_0331E_02317_R5_P

Related

Ltrim trimming extra character

I have the below code:
SELECT
ltrim('REASON_ACTIVE_DCA', 'REASON_') reason
FROM
dual
However, I'm obtaining '_CTIVE_DCA'. What's happening and how can I get 'ACTIVE_DCA' with ltrim?
Because LTRIM() removes all the characters as a set. So all leading "R"s and "E"s and so on removed. In fact, the ordering of the characters in the second string is irrelevant, so you would get the same result with '_NOSAER'.
If you want to remove the leading string of REASON_ -- if present -- then you don't use trim(). Instead, one method is:
select (case when 'REASON_ACTIVE_DCA' LIKE 'REASON$_%' ESCAPE '$'
then substr('REASON_ACTIVE_DCA', 8)
else 'REASON_ACTIVE_DCA'
end)
There are other ways, such as:
select regexp_replace('REASON_ACTIVE_DCA', '^REASON_', '')
I would do it with regular string functions (not regular expressions), and using INSTR instead of LIKE so I don't have to worry about escaping underscore.
Something like this - including a few sample strings in the WITH clause for testing:
with
inputs (i_str) as (
select 'REASON_ACTIVE_DCA' from dual union all
select 'REASON_NOT_GIVEN' from dual union all
select null from dual union all
select 'REASON-SPECIAL' from dual union all
select 'REASON_' from dual union all
select 'REASON' from dual
)
select i_str, substr(i_str, case instr(i_str, 'REASON_')
when 1 then 1 + length('REASON_')
else 1 end) as new_str
from inputs;
I_STR NEW_STR
----------------- -----------------
REASON_ACTIVE_DCA ACTIVE_DCA
REASON_NOT_GIVEN NOT_GIVEN
REASON-SPECIAL REASON-SPECIAL
REASON_
REASON REASON

regexp match character before a specify string (not include)

REGEXP_SUBSTR(label) ,'.*_dis')
this is for sql;
my database is mysql
select REGEXP_SUBSTR(label) ,'.*_dis') as dis ,
substr(label,length(label))-1) as num
from table
table.lable col's data:
1. a_b_dis_12
2. a_dis_13
3. c_d_dis_23
3. c_dis_22
i want to get the character before '_dis' and the numeric part use regexp
1.a_b 12
2.a 13
3.c_d 23
4.c 22
thanks a lot!
You can use regexp_substr as follows:
Select regexp_substr(your_column, '^(.*)_dis_[0-9]+$',1,1,null,1) as dis,
Regexp_substr(your_column, '[0-9]+$') as num
From your table
You can use regexp_replace():
select regexp_replace(col, '^(.*)_dis.*$', '\1'),
regexp_replace(col, '^.*_dis_([0-9]+)', '\1')
from (select 'a_b_dis_12' as col from dual union all
select 'a_dis_13' as col from dual union all
select 'c_d_dis_23' as col from dual union all
select 'c_dis_22' as col from dual
) t;
Here is a db<>fiddle.
I would use regexp_replace() as follows:
select
regexp_replace(label, '_dis_.*$', '') dis,
regexp_replace(label, '^.*_dis_', '') num
from mytable
The first expression suppresses everything from '_dis_ (included) to the end of the string. The second expression removes everything from the beginning of the string until '_dis_' (included).

Extracting substring in Oracle

Let's say I have three rows with value as
1 121/2808B|:6081
2 OD308B|:6081_1:
3 008312100001200|:6081_1
I want to display value only until B but want to exclude everything after B. So as you can see in above data:
from 121/2808B|:6081 I want only 121/2808B
from OD308B|:6081_1: only OD308B
from 008312100001200|:6081_1 only 008312100001200.
Thanks for the Help.
Try this: regexp_substr('<Your_string>','[^B]+')
SELECT
REGEXP_SUBSTR('121/2808B|:6081', '[^B]+')
FROM
DUAL;
REGEXP_S
--------
121/2808
SELECT
REGEXP_SUBSTR('OD308B|:6081_1:', '[^B]+')
FROM
DUAL;
REGEX
-----
OD308
SELECT
REGEXP_SUBSTR('008312100001200.', '[^B]+')
FROM
DUAL;
REGEXP_SUBSTR('0
----------------
008312100001200.
db<>fiddle demo
Cheers!!
You could try using SUBSTR() and INSTR()
select SUBSTR('121/2808B|:6081',1,INSTR('121/2808B|:6081','B', 1, 1) -1)
from DUAL
I think you forgot to mention that you wanted to use | as a field separator, but I deduced this from the expected result from the third string. As such the following should give you what you want:
WITH cteData AS (SELECT 1 AS ID, '121/2808B|:6081' AS STRING FROM DUAL UNION ALL
SELECT 2, 'OD308B|:6081_1:' FROM DUAL UNION ALL
SELECT 3, '008312100001200|:6081_1' FROM DUAL)
SELECT ID, STRING, SUBSTR(STRING, 1, CASE
WHEN INSTR(STRING, 'B') = 0 THEN INSTR(STRING, '|')-1
ELSE INSTR(STRING, 'B')-1
END) AS UP_TO_B
FROM cteData;
dbfiddle here
Assuming Bob Jarvis is correct in the assumption that "|" is also a delimiter (as seems likely) try:
-- define test data
with test as
( select '121/2808B|:6081' stg from dual union all
select 'OD308B|:6081_1:' from dual union all
select '008312100001200|:6081_1' from dual
)
-- execute extract
select regexp_substr(stg , '[^B|]+') val
from test ;

Substring Regular Expression for occurence

I want to select part of the string which occurs after the first underline _ and before the second, third or whatever amount of underlines _ occur in a string.
For example I have strings such as:
75618_LORIK1_2_BABA_ODD_GENERIC
19_GENTRIT3_CC_DD_FF_BROWSERTC
75618_BETIM2
Output should be:
LORIK1
GENTRIT3
BETIM2
I cant seem to find some kind of expression of substring to get that part, I tried using:
SELECT SUBSTR(COLNAME, 0, INSTR(COLNAME, '_')-1) FROM DUAL;
But it seems to get only the part before the first occurrence of '_'.
Here's one way to do this with regular expressions.
with
test_data (str) as (
select '75618_LORIK1_2_BABA_ODD_GENERIC' from dual union all
select '19_GENTRIT3_CC_DD_FF_BROWSERTC' from dual union all
select '75618_BETIM2' from dual union all
select 'NO UNDERLINES HERE' from dual
)
select str, regexp_substr(str, '[^_]*', 1, 3) as second_token
from test_data
;
STR SECOND_TOKEN
------------------------------- -------------------------------
75618_LORIK1_2_BABA_ODD_GENERIC LORIK1
19_GENTRIT3_CC_DD_FF_BROWSERTC GENTRIT3
75618_BETIM2 BETIM2
NO UNDERLINES HERE

Get substring with REGEXP_SUBSTR

I need to use regexp_substr, but I can't use it properly
I have column (l.id) with numbers, for example:
1234567891123!123 EXPECTED OUTPUT: 1234567891123
123456789112!123 EXPECTED OUTPUT: 123456789112
12345678911!123 EXPECTED OUTPUT: 12345678911
1234567891123!123 EXPECTED OUTPUT: 1234567891123
I want use regexp_substr before the exclamation mark (!)
SELECT REGEXP_SUBSTR(l.id,'[%!]',1,13) from l.table
is it ok ?
You can try using INSTR() and substr()
DEMO
select substr(l.id,1,INSTR(l.id,'!', 1, 1)-1) from dual
You want to remove the exclamation mark and all following characters it seems. That is simply:
select regexp_replace(id, '!.*', '') from mytable;
Look at it like a delimited string where the bang is the delimiter and you want the first element, even if it is NULL. Make sure to test all possibilities, even the unexpected ones (ALWAYS expect the unexpected)! Here the assumption is if there is no delimiter you'll want what's there.
The regex returns the first element followed by a bang or the end of the line. Note this form of the regex handles a NULL first element.
SQL> with tbl(id, str) as (
select 1, '1234567891123!123' from dual union all
select 2, '123456789112!123' from dual union all
select 3, '12345678911!123' from dual union all
select 4, '1234567891123!123' from dual union all
select 5, '!123' from dual union all
select 6, '123!' from dual union all
select 7, '' from dual union all
select 8, '12345' from dual
)
select id, regexp_substr(str, '(.*?)(!|$)', 1, 1, NULL, 1)
from tbl
order by id;
ID REGEXP_SUBSTR(STR
---------- -----------------
1 1234567891123
2 123456789112
3 12345678911
4 1234567891123
5
6 123
7
8 12345
8 rows selected.
SQL>
If you like to use REGEXP_SUBSTR rather than regexp_replace then you can use
SELECT REGEXP_SUBSTR(l.id,'^\d+')
assuming you have only numbers before !
If I understand correctly, this is the pattern that you want:
SELECT REGEXP_SUBSTR(l.id,'^[^!]+', 1)
FROM (SELECT '1234567891123!123' as id from dual) l