Selecting rows where column having number as substring - sql

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

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 get integer from column without using regexp_like

I have a Column X in Oracle that has values like "a1b2c3", "abc", "1ab", "123", "156-346"
how do I write a sql query that returns me only the X that hold pure numerical values like 123,456 etc.
I can not using regexp_like because I am using 10g.
Here are two options: one uses regular expressions, another translate function.
SQL> with test as
2 (select 'a1b2c3' col from dual union all
3 select 'abc' from dual union all
4 select '1ab' from dual union all
5 select '123' from dual union all
6 select '156-346' from dual
7 )
8 select col,
9 regexp_replace(col, '[^[:digit:]]') result,
10 --
11 translate(col, '0'|| translate(col, '$0123456789', '$'), '0') result2
12 from test;
COL RESULT RESULT2
------- ------- -------
a1b2c3 123 123
abc
1ab 1 1
123 123 123
156-346 156346 156346
SQL>
When using the translate function to remove all the digits and trimming the result, pure numbers will yield null
select x
from test
where trim(translate(x, '0123456789', ' ')) is null;
Make sure to have 10 spaces as last parameter to translate.
If the x column can contain spaces, then use
select x
from test
where trim(translate(replace(x,' ','*'), '0123456789', ' ')) is null;
see: http://sqlfiddle.com/#!4/772dc7d/2/0

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.

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