Regexp strings with specific characters - sql

I would like to create a query where I select all records which may contain letters, digits and special characters from group of 3 special characters: / " ,
I've tried '[0-9a-zA-Z(\-\/\")]' but something like 'a+' works

Try this regex:
[0-9a-zA-Z/",]+

Something like this?
SQL> with test (col) as
2 (-- valid values
3 select 'Little12' from dual union all
4 select 'Foot/15' from dual union all
5 select '"London",UK' from dual union all
6 -- invalid values
7 select '25+ miles' from dual union all
8 select 'me#gmail.com' from dual
9 )
10 select col
11 from test
12 where regexp_like(col, '^[a-zA-Z0-9/",]*$');
COL
------------
Little12
Foot/15
"London",UK
SQL>

Related

How to search for a name in where Oracle with string and number character

I need to find records in Oracle that have only such record like 'string|number|number|number|number|number' for example - 'A12345'
select * from base where column_x like 'string|number|number|number|number|number'
That's a letter followed by 5 digits:
SQL> with test (col) as
2 (select 'A12345' from dual union all
3 select 'B123CD' from dual union all
4 select '123ABC' from dual
5 )
6 select *
7 from test
8 where regexp_like(col, '^[[:alpha:]]{1}[[:digit:]]{5}$');
COL
------
A12345
SQL>

How to find a row where col have alphabets,numbers or special characters (except hyphen,apostrophe and space) in Oracle SQL

I have to find the name like : Robert#jr23 (There must be Alphabet,Any Special characters or Numbers except hyphen(-),apostrophe (') and Space).
I was doing as below:
select * from test where REGEXP_LIKE(trim(NAME_1), '[^- '']')
But I am not getting the right results with this.
Need to match:
Kevin#123
bob#jr
mike$dr
Needs to exclude:
Alex-jr
Robert'jr
Brian jr
You need to use not with a pattern matching the values you want to exclude. Otherwise you are matching strings that contain any character that is not in the exclusion list, which is all of them.
select column_value
from ora_mining_varchar2_nt
( 'Kevin#123'
, 'bob#jr'
, 'mike$dr'
, 'Alex-jr'
, 'Robert''jr'
, 'Brian jr'
, 'A!' )
where 1=1
and not regexp_like(column_value,'[- '']')
and regexp_like
(column_value, '([A-Z0-9]+[^A-Z0-9])|([^A-Z0-9]+[A-Z0-9])', 'i') ;
Edit: added a regex_like condition to ensure that values contain a letter or digit and a 'special character', meaning here a character that is neither a letter, digit or space, ' or -.
You can use following query to include all special characters except space, - and '
SQL> with tbl(name) as (
2 select 'Kevin#123' from dual union
3 select 'bob#jr' from dual union
4 select 'mike$dr' from dual union
5 select 'Alex-jr' from dual union
6 select 'Brian jr' from dual union
7 select 'Brian)jr' from dual union
8 select 'Robert''jr' from dual
9 )
10 select *
11 from tbl
12 where regexp_like(name, '[^[a-z]|[A-Z]|[:space:]|[:cntrl:]|/]')
13 and not regexp_like(name,'[- '']');
NAME
---------
Brian)jr -- see this is included
Kevin#123
bob#jr
mike$dr
SQL>
One option is to replace everything that's valid with NULL, and what remains is invalid:
SQL> with test (col) as
2 (select 'Robert#jr23' from dual union all
3 select 'Kevin#123' from dual union all
4 select 'bob#jr' from dual union all
5 select 'mike$dr' from dual union all
6 select 'Alex-jr' from dual union all
7 select 'Robert''jr' from dual union all
8 select 'Brian jr' from dual
9 )
10 select col
11 From test
12 where regexp_replace(col, '[[:alpha:]]|[[:digit:]]|-|''', null) is not null;
COL
-----------
Robert#jr23
Kevin#123
bob#jr
mike$dr
Brian jr
SQL>
I'm confused about what you, actually, want to get as a result. Here are some examples which show result of such a regular expression; which IDs do you want to get as a result?
SQL> with test (id, col) as
2 (select 1, 'Robert#jr23' from dual union all
3 select 2, 'Kevin#123' from dual union all
4 select 3, 'bob#jr' from dual union all
5 select 4, 'mike$dr' from dual union all
6 select 5, 'Alex-jr' from dual union all
7 select 6, 'Robert''jr' from dual union all
8 select 7, 'Brian jr' from dual union all
9 select 8, 'Brian10' from dual
10 )
11 select col,
12 regexp_replace(col,
13 '[[:alpha:]]|[[:digit:]]|[[:space:]]|-|''', null) result
14 from test;
COL RESULT
----------- ----------
Robert#jr23 #
Kevin#123 #
bob#jr #
mike$dr $
Alex-jr
Robert'jr
Brian jr
Brian10
8 rows selected.
SQL>
One of your comments says:
select a customer name where there must be a special character or numbers (except space, Hyphen and apostrophe)
which means that numbers and special characters should be treated as "equal". If that's so, does this help?
SQL> with test (id, col) as
2 (select 1, 'Robert#jr23' from dual union all
3 select 2, 'Kevin#123' from dual union all
4 select 3, 'bob#jr' from dual union all
5 select 4, 'mike$dr' from dual union all
6 select 5, 'Alex-jr' from dual union all
7 select 6, 'Robert''jr' from dual union all
8 select 7, 'Brian jr' from dual union all
9 select 8, 'Brian10' from dual
10 )
11 select id, col
12 from test
13 where regexp_replace(col, '[[:alpha:]]|[[:space:]]|-|''', '')
14 is not null;
ID COL
---------- -----------
1 Robert#jr23
2 Kevin#123
3 bob#jr
4 mike$dr
8 Brian10
SQL>

using regular expression that not include string '05613'. have NAW_05613_11_PL04_02 in table. need condition rows which have 05613 not be displayed

I want to fetch data from database using regular expression which should not include string '05613'.
I have values like NAW_05613_11_PL04_02 in my table. I want to give condition that all rows which have 05613 in it should not be displayed.
I have tried using REGEXP_LIKE(d.variable_value_string, '^N(*)[^{05613}](*)')
SELECT * from tablename where REGEXP_LIKE(columnname, '^N(*)[^{05613}](*)')
Expected result should be- row with value having 05613 should not be retrieved.
A simple LIKE could be enough:
... columnName not like '%05613%'
For example:
SQL> with test(c) as (
2 select '05613XX' from dual union all
3 select 'XX05613' from dual union all
4 select 'X05613X' from dual union all
5 select 'XXX' from dual
6 )
7 select *
8 from test
9 where c not like '%05613%';
C
-------
XXX
SQL>
If you need, for some reason, regexp_like, this is a way:
SQL> with test(c) as (
2 select '05613XX' from dual union all
3 select 'XX05613' from dual union all
4 select 'XX05613' from dual union all
5 select 'X05613X' from dual union all
6 select 'XXX' from dual
7 )
8 select *
9 from test
10 where not regexp_like(c, '05613');
C
-------
XXX
If this is your assignment and usage of regexp_like is mandated then use following regexp_like:
SELECT * from tablename D
where Not REGEXP_LIKE(d.variable_value_string, '05613')
Cheers!!

Regular expression matching with Oracle

I am working on SQL Developer. I want only those records which have non-numeric data. The query I used is:
select * from TBL_NAME where regexp_like (mapping_name,'%[!0-9]%');
Strangely this is not working.
How about this? As you said, return values that are NOT numbers.
SQL> with test (col) as
2 (select 'abc123' from dual union
3 select '12345' from dual union
4 select 'abc' from dual union
5 select '($ff3' from dual union
6 select '12.345' from dual
7 )
8 select col
9 from test
10 where not regexp_like (col, '^\d+|(\.\d+)$');
COL
------
($ff3
abc
abc123
SQL>
If there are no decimal values, regular expression is even simpler: '^\d+$'
[EDIT, after sample data have been provided]
Piece of cake:
SQL> with test (col) as
2 (select 'ABC' from dual union
3 select 'BCE1' from dual union
4 select '2GHY' from dual union
5 select 'WE56S' from dual union
6 select 'TUY' from dual
7 )
8 select col
9 from test
10 where not regexp_like (col, '\d');
COL
-----
ABC
TUY
SQL>

Selecting rows where column having number as substring

I have to write a query to fetch all the rows with a column containing numbers as substring
Data
------
abc123
defgh
wz127bdn
Now my desired result is
Result
-----
abc123
wz127bdn
I wrote the query like
SELECT data
FROM table
WHERE data like '%[0-9]%'
But this is not fetching the result.
You need a REGEXP_LIKE:
SQL> with test(data) as (
2 select 'abc123' from dual union all
3 select 'defgh' from dual union all
4 select 'wz127bdn' from dual union all
5 select '[0-9]' from dual
6 )
7 select *
8 from test
9 where regexp_like(data, '[0-9]')
10 ;
DATA
--------
abc123
wz127bdn
[0-9]
LIKE will not interpret '[0-9]' as "look for a digit", but exactly as you write it, thus searching for the string '[0-9]':
SQL> with test(data) as (
2 select 'abc123' from dual union all
3 select 'defgh' from dual union all
4 select 'wz127bdn' from dual union all
5 select '[0-9]' from dual
6 )
7 select *
8 from test
9 where data like '%[0-9]%' ;
DATA
--------
[0-9]
Use regexp_like() in Oracle:
SELECT data
FROM table
WHERE regexp_like(data, '[0-9]');
Note that the wildcards are not necessary, because regular expressions match anywhere in the string. If you like, you can do:
WHERE regexp_like(data, '.*[0-9].*');