How to update a columns having a specific string patter/sequence - regex

There is a column in my table having values of a pattern like 'A=xxx^B=xxx^C=xxx^D=xxx^' i need to update all the columns having this pattern to a pattern like 'C=xxx^D=xxx^', where x is a number.

Would something like this help? REGEXP_LIKE returns rows which satisfy the condition, while an ordinary SUBSTR returns the desired result.
SQL> with test (col) as
2 (select 'A=123^B=123^C=123^D=123^' from dual union
3 select 'A=123^B=456^C=789^D=987^' from dual union
4 select 'A=333^C=333^D=333^' from dual union
5 select 'C=987^D=987^' from dual union
6 select 'B=876^' from dual union
7 select 'A=123^B=123^C=123^D=123^E=123^' from dual
8 )
9 select col,
10 substr(col, instr(col, 'C')) result
11 from test
12 where regexp_like(col, '^A=\d+{3}\^B=\d+{3}\^C=\d+{3}\^D=\d+{3}\^$');
COL RESULT
------------------------------ ------------------------------
A=123^B=123^C=123^D=123^ C=123^D=123^
A=123^B=456^C=789^D=987^ C=789^D=987^
SQL>

I managed to come up with a solution since i'm looking for a pattern which starts from 'A=' i used REGEXP_LIKE to find that particular pattern. Then i used SUBSTR to extract the value from the string which should start from the 2nd '^' character.
Update MYTABLE t set t.key = SUBSTR(t.key,INSTR(t.key,'^',1,2)+1) WHERE REGEXP_LIKE(t.key_ref,'^A=') and t.dno = 'xxxxx';

Related

check if string has the format : 2 lettes au max following with numbers with regexp_like oracle

Could you please tell me the expression regexp_like to check if a string has the format which begins with one or two letters max followed by numbers. For example, like 'A25', 'AB567'.
Thanks
How about
SQL> with test (col) as
2 (select 'A25' from dual union all
3 select 'AB567' from dual union all
4 select 'A12A532A' from dual union all
5 select 'ABC123' from dual union all
6 select '12XYZ34' from dual
7 )
8 select col,
9 case when regexp_like(col, '^[[:alpha:]]{1,2}[[:digit:]]+$') then 'OK'
10 else 'Wrong'
11 end result
12 from test;
COL RESULT
---------- ----------
A25 OK
AB567 OK
A12A532A Wrong
ABC123 Wrong
12XYZ34 Wrong
SQL>
The regular expression would be ^[A-Z]{1,2}[0-9]+$.
Working demo on db<>fiddle here
This match a string that contain only numbers and letters, that start with a letter, when there is never more than 2 letters in a row.
^(([A-Za-z]){1,2}([0-9])+)*\2{0,2}$

Regular Expression patterns in Oracle SQL developer: include numbers

I am trying to find the pattern using oracle sql developer. Include numbers that are "x0000"," 0000",".0000"
AND REGEXP_INSTR(string=>FTP.TEXT, pattern=>'[x]\d\d\d\d\s', match_parameter=>'i')
Use
where REGEXP_LIKE(str,'[x.[:space:]]\d{4}','i')
The [x.[:space:]]\d{4} expression matches x, . or any whitespace, then any four digit characters.
You can use REGEXP_LIKE as follows:
SQL> --sample data
SQL> with your_data(id,str) as
2 (select 1, 'x0000' from dual union all
3 select 2, '.0000' from dual union all
4 select 3, ' 0000'from dual union all
5 select 4, '0000' from dual) -- this should not be in the result
6 -- your query starts from here
7 select * from your_data
8 where REGEXP_like(str,'[x| |\.][0-9]{4}','i');
ID STR
---------- -----
1 x0000
2 .0000
3 0000
SQL>

matching patterns for regexp_like in Oracle to include a group of character conditionally

I tried to look up for a good documentation for matching pattern for use of regexp_like in Oracle. I have found some and followed their instructions but looks like I have missed something or the instruction is not comprehensive.
Let's look at this example:
SELECT * FROM
(
SELECT 'ABC' T FROM DUAL
UNION
SELECT 'WZY' T FROM DUAL
UNION
SELECT 'WZY_' T FROM DUAL
UNION
SELECT 'WZYEFG' T FROM DUAL
UNION
SELECT 'WZY_EFG' T FROM DUAL
) C
WHERE regexp_like(T, '(^WZY)+[_]{0,1}+[A-Z]{0,6}')
What I expect to receive are WZY and WZY_EFG. But what I got was:
What I would like to have is the "_" could be present or not but if there are character after the first group, it is mandatory that it be present only once.
Is there a clean way to do this?
Use a subexpression grouping to make sure the _ character appears only with Capitalized Alphabetical Characters
Yes, your pattern does not address the conditional logic you need (only see the _ when capitalized alphabetical characters follow).
Placing the _ character in with a capitalized alphabetical character list into a subexpression grouping forces this logic.
Finally, placing the end of line anchor addresses the zero match scenarios.
SCOTT#DB>SELECT
2 *
3 FROM
4 (
5 SELECT 'ABC' t FROM dual
6 UNION ALL
7 SELECT 'WZY' t FROM dual
8 UNION ALL
9 SELECT 'WZY_' t FROM dual
10 UNION ALL
11 SELECT 'WZYEFG' t FROM dual
12 UNION ALL
13 SELECT 'WZY_EFG' t FROM dual
14 ) c
15 WHERE
16 REGEXP_LIKE ( t, '^(WZY)+([_][A-Z]{1,6}){0,1}$' );
T
__________
WZY
WZY_EFG

Regex extract positive or negative number from string oracle

I am struggling in finding the solution to the following problem:
Assuming the character values 512a, -1230b, -2 and 2.
Which can be obtained into a table from the following query:
with my_input_values as (
select '512a' my_val from dual union select '-1230b' my_val from dual union select '2' my_val from dual union select '-2' my_val from dual
)
select * from my_input_values;
I am trying to build the regular expression that extract the number keeping the positive or negative sign from each value.
The expected result are the following numeric values: 512, -1230, 2 and -2.
Which can be obtained into a table through the following query:
with result as (
select 512 my_val from dual union select -1230 my_val from dual union select 2 my_val from dual union select -2 my_val from dual
)
select * from result;
You could match an optional character (or use an asterix * instead of ? to match zero or more characters) followed by a quote. Then you could replace that match with an empty string.
[a-z]?'
For completeness of the answer, the Oracle code to accomplish this task is the following:
select regexp_replace('-asd51assaddasd2a', '[a-z]?') from dual;
which successfully results in a numberic field maintaining the polarity sign
select -512 from dual;

Query for regular expression - oracle DB

I am trying to get all the name which has special char and numbers in it. I want to know if there is a better way of writing this query. Thanks
select Name from TABLE1
WHERE NAME LIKE '%-%'
OR NAME LIKE '%$%'
OR NAME LIKE '%4%' etc
Try this:
SELECT Name FROM Test WHERE REGEXP_LIKE(Name,'(-|$|4)');
The | (OR or Alternation) operator allows the expression to return true if the value contains an '-' OR an '$' OR an '4' etc.
Use square brackets to define a character class. Also a full regex to anchor the pattern to the beginning of the string, any number of any characters before a character class consisting of a dash or a 4 or a dollar sign then any number of any characters anchored to the end of the string:
SQL> with tbl(name) as (
2 select 'efs' from dual
3 union
4 select 'abc-' from dual
5 union
6 select 'a4bd' from dual
7 union
8 select 'gh$dll' from dual
9 union
10 select 'xy5zzy' from dual
11 )
12 select name from tbl
13 where regexp_like(name, '^.*[-|$|4].*$');
NAME
------
a4bd
abc-
gh$dll
SQL>