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 - sql

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!!

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>

Regexp strings with specific characters

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>

sql query for like to Order exact match at top in list

sql query for like to show exact match first in list:
dataset: "abcd", "a","b","bc","bcd"
select * from table where data like "%bc%";
it should show in order bc, abcd, bcd.
As you said - sort by match.
SQL> with test (col) as
2 (select 'abcd' from dual union all
3 select 'a' from dual union all
4 select 'b' from dual union all
5 select 'bc' from dual union all
6 select 'bcd' from dual
7 )
8 select col
9 from test
10 where col like '%' || '&&par_search_string' ||'%'
11 order by utl_match.jaro_winkler_similarity(col, '&&par_search_string') desc;
Enter value for par_search_string: bc
COL
----
bc
bcd
abcd
SQL> undefine par_search_string
SQL> /
Enter value for par_search_string: cd
COL
----
bcd
abcd
SQL>
One of many methods:
with
t as (
select 'abcd' c from dual union all
select 'a' from dual union all
select 'b' from dual union all
select 'bc' from dual union all
select 'bcd' from dual
)
select *
from t
where c like '%bc%'
order by length(c)
Demo.
I think you can use a query like this one that can return your expected result.
select * from table where data='bc'
union all
select * from table where data like '%bc%' and data<>'bc'
One method is:
select *
from t
where data like '%bc%'
order by (case when data = 'bc' then 1 else 2 end);
Or if you don't want to type so much:
order by nullif(data, 'bc') desc
The descending sort puts NULL values first.

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].*');