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;
Related
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
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';
I have table in Oracle DB. For example tbl1. And 1 column. For Example col1. My col1 - column text type. I need select all rows where text is 0 and It occurs once in the text. And if text It contains >1 digits I do not need it. For example i need this rows:
0
text0
0text
text 0 text
text0 text
and I do not need this rows
only text
0 0
00
10
3243455
0text 1
I think I need regex but I do not know how to use them.
I write
select * from tbl1 t where regexp_like(t.col1,'[0]{1}')
but i get only rows where contains 0
[^0-9]*[0-9][^0-9]*
This means: any non-digit any number of times, then a digit exactly once, and then any non-digit any number of times.
You might need to add ^ and $ to force it to match the entire string:
^[^0-9]*[0-9][^0-9]*$
Based on the answer by Ilya Kogan, you could get the rows you want with the following regular expression:
WITH tbl1 AS (SELECT '0' col1 FROM dual
UNION
SELECT 'text0' col1 FROM dual
UNION
SELECT '0text' col1 FROM dual
UNION
SELECT 'text 0 text' col1 FROM dual
UNION
SELECT 'text0 text ' col1 FROM dual
UNION
SELECT 'only text' col1 FROM dual
UNION
SELECT '0 0' col1 FROM dual
UNION
SELECT '00' col1 FROM dual
UNION
SELECT '10' col1 FROM dual
UNION
SELECT '3243455' col1 FROM dual
UNION
SELECT '0text 1' col1 FROM dual)
SELECT COL1, REGEXP_SUBSTR(col1,'\A\D*0\D*\Z')
FROM tbl1
WHERE REGEXP_LIKE(col1,'\A\D*0\D*\Z')
Where:
\A is the beginning of the line.
\D is a non-digit character.
0 is the character for the number 0.
\Z is the end of the line.
I have a input column with below values.
1.2%
111.00$
$100.00
aa
ss
Output expected
1.2
111.00
100.00
null
null
I am trying to use REGEXP_REPLACE and tried replacing every character that is not a digit or "." so that 1.2% will become 1.2.
Here is the query I tried but this didn't work.
regexp_replace('%1.aa2', '[^[\d|\.]]', '')
Can anyone suggest how to do that? and what I am doing wrong? I am working on Oracle 11.2 database with pl/sql developer.
Use POSIX class for matching digit chars ie [:digit:]. So this negated class [^[:digit:].] should match any character but not of dot or digit.
regexp_replace('%1.aa2', '[^[:digit:].]', '')
You can use the [^0-9.] regex and replace with empty string:
with testdata(txt) as (
select 'ss' from dual
union
select 'aa' from dual
union
select '$100.00' from dual
union
select '111.00$' from dual
union
select '1.2%' from dual
)
select regexp_replace(txt, '[^0-9.]', '')
from testdata;
Result of an SQL fiddle:
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>