How to search for a word between '' in SQL Table - sql

What I'm trying to do is to search for a word in SQL between two ' like 'test' but it won't work using:
SELECT id From TestTable WHERE Test LIKE '%'test'%'

The following SQL statement selects all id from TestTable Where the Test column has "test" in any position:
SELECT id From TestTable WHERE Test LIKE '%test%';
Let's say if the text in TestTable is this: "this is a simple 'test'"
and you are looking for 'test' (not test) then your query will look like this:
SELECT id From TestTable WHERE Test LIKE "%'test'%";
As per my experience, sometimes outer quotes conflict with inner quotes. So I prefer To use different quotes for the outer and inner sides in like statement.

Oracle
Here you can see the differences:
WITH
a_tbl AS
(
Select 1 "ID", 'This is a test string 1' "TEST_STRING" From Dual Union ALL
Select 2 "ID", 'This is a ''test'' string 2' "TEST_STRING" From Dual Union ALL
Select 3 "ID", 'This is a "test" string 3' "TEST_STRING" From Dual Union ALL
Select 4 "ID", 'This is a string 4 - without magic word' "TEST_STRING" From Dual Union ALL
Select 5 "ID", 'This is a string 5 - just for testing' "TEST_STRING" From Dual
)
/* Sample data
ID TEST_STRING
---------- ---------------------------------------
1 This is a test string 1
2 This is a 'test' string 2
3 This is a "test" string 3
4 This is a string 4 - without magic word
5 This is a string 5 - just for testing
*/
Select * From a_tbl Where TEST_STRING Like '%test%'
/* Result
ID TEST_STRING
---------- ---------------------------------------
1 This is a test string 1
2 This is a 'test' string 2
3 This is a "test" string 3
5 This is a string 5 - just for testing
*/
Select * From a_tbl Where TEST_STRING Like '%''test''%'
/* Result
ID TEST_STRING
---------- ---------------------------------------
2 This is a 'test' string 2
*/
Select * From a_tbl Where TEST_STRING Like '%"test"%'
/* Result
ID TEST_STRING
---------- ---------------------------------------
3 This is a "test" string 3
*/
Note that '%test%' will select rows containing something like testing or testament and if you want just the word test then use surrounding spaces LIKE '% test %'

Related

REGEXP to validate a specific number

How can I search for a specific number in an array using REGEXP?
I have an array and need to verify if it has a specific number.
Ex: [5,2,1,4,6,19] and I am looking for number 1, but just the number 1 and not any number that contain the digit 1.
I had to do this:
case when REGEXP_INSTR(JSON_QUERY(MY_JSON_COLUMN,'$.path') , '[[]{1}[1][,]')<>0
or REGEXP_INSTR(JSON_QUERY(MY_JSON_COLUMN,'$.path') , '[,]{1}[1][,]{1}')<>0
or REGEXP_INSTR(JSON_QUERY(MY_JSON_COLUMN,'$.path') , '[,]{1}[1][]]')<>0
or REGEXP_INSTR(JSON_QUERY(MY_JSON_COLUMN,'$.path') , '[[]{1}[1][]]') <>0
then 'DIGIT_ONE' else 'NO_DIGIT_ONE'
end
Is there anything simpler?
You can use
(^|\D)1(\D|$)
This will seach for 1 not enclosed with other digits.
See this regex demo.
Details
(^|\D) - start of string or non-digit
1 - a 1 char
(\D|$) - non-digit or end of string.
Do NOT use regular expressions, use a proper JSON parser and then filter for the number you want:
SELECT my_json_column,
CASE
WHEN JSON_EXISTS( my_json_column, '$?(#.path[*] == 1)' )
THEN 'DIGIT ONE'
ELSE 'NO DIGIT ONE'
END AS has_one
FROM table_name;
or (if you are using Oracle 12.1 and cannot use path filter expressions with JSON_EXISTS, which is only available from Oracle 12.2):
SELECT my_json_column,
CASE
WHEN EXISTS(
SELECT 'X'
FROM JSON_TABLE(
t.my_json_column,
'$.path[*]'
COLUMNS (
value NUMBER PATH '$'
)
)
WHERE value = 1
)
THEN 'DIGIT ONE'
ELSE 'NO DIGIT ONE'
END
FROM table_name t;
Which, for the sample data:
CREATE TABLE table_name (
my_json_column CHECK ( my_json_column IS JSON )
) AS
SELECT '{"path":[5,2,1,4,6,19],"not_this_path":[1,2,3,4,5]}' FROM DUAL UNION ALL
SELECT '{"path":[5,2,4,6,19],"not_this_path":[1,2,3,4,5]}' FROM DUAL UNION ALL
SELECT '{"path":[11],"not_this_path":[1]}' FROM DUAL UNION ALL
SELECT '{"path":[2],"not_this_path":[1]}' FROM DUAL UNION ALL
SELECT '{"path":[1,11]}' FROM DUAL;
Both output:
MY_JSON_COLUMN | HAS_ONE
:-------------------------------------------------- | :-----------
{"path":[5,2,1,4,6,19],"not_this_path":[1,2,3,4,5]} | DIGIT ONE
{"path":[5,2,4,6,19],"not_this_path":[1,2,3,4,5]} | NO DIGIT ONE
{"path":[11],"not_this_path":[1]} | NO DIGIT ONE
{"path":[2],"not_this_path":[1]} | NO DIGIT ONE
{"path":[1,11]} | DIGIT ONE
db<>fiddle here
Alternatively, with a little bit more typing (a little bit? Am I kidding?!), splitting the string into rows and comparing values to the search string:
SQL> with test (col) as
2 (select '[5,2,1,4,6,19]' from dual)
3 select t.col,
4 case when '&par_search_string' in
5 (select regexp_substr(substr(col, 2, length(col) - 1), '[^,]+', 1, level) val
6 from test
7 connect by level <= regexp_count(col, ',') + 1
8 )
9 then 'Search string exists'
10 else 'Search string does not exist'
11 end result
12 from test t;
Enter value for par_search_string: 1
COL RESULT
-------------- ----------------------------
[5,2,1,4,6,19] Search string exists
SQL> /
Enter value for par_search_string: 24
COL RESULT
-------------- ----------------------------
[5,2,1,4,6,19] Search string does not exist
SQL>

How to remove one or more instances of '?*' in a string using REGEXP_REPLACE function in oracle?

I'm getting the characters '?*' one to three times in a column called Line. I am required to remove these characters. How do I do it using Replace or REGEXP_REPLACE?
SELECT
Line, REGEXP_REPLACE(LINE,'[?*]','')--([^\+]+)
FROM
TABLE
WHERE
INSTR(LINE,'?*') != 0;
where
REGEXP_REPLACE(LINE,'\?*','') replaces the ? alone and leaves the * untouched.
REGEXP_REPLACE(LINE,'?*','') replaces nothing.
REGEXP_REPLACE(LINE,'[?*]','') replaces all ?s and all *s. I am only replacing when ? and * comes together as ?*.
If you need the remove the string '?*', you can use replace:
SQL> with test(string) as (
2 select 'aa?*b?' from dual union all
3 select 'a*a?*??b?' from dual union all
4 select 'a*a??b???c*?**cc' from dual union all
5 select 'aa?b?*?cc?d??*?*?' from dual
6 )
7 select string, replace(string, '?*', '') as result
8 from test;
STRING RESULT
----------------- ---------------
aa?*b? aab?
a*a?*??b? a*a??b?
a*a??b???c*?**cc a*a??b???c**cc
aa?b?*?cc?d??*?*? aa?b?cc?d??
Use (\?\*) as pattern :
with tab(line) as
(
select 'abc?*ghh*?g?l*' from dual union all
select '?*U?KJ*H' from dual union all
select '*R5?4*&t?*frg?*' from dual
)
select regexp_replace(line,'(\?\*)') as "Result String"
from tab;
Result String
-------------
abcghh*?g?l*
U?KJ*H
*R5?4*&tfrg
Demo

Replace a column value in a string delimiter using Oracle SQL

I have a string value in a column in a table like
001|3880000005376|Personal ID| ||15-MAY-2006
and I want to replace the fourth value by another string value 'ABCDEF' , can it be possible by a single update or by PL/SQL program?
Here's one option:
SQL> with test (id, col) as
2 (select 1, '001|3880000005376|Personal ID| ||15-MAY-2006' from dual union all
3 select 2, '002|3880000005376|Personal ID|XXX||15-MAY-2007' from dual
4 )
5 select
6 id,
7 regexp_replace(col, '[^|]+', 'NEW STRING', 1, 4) result
8 from test;
ID RESULT
---------- ------------------------------------------------------------
1 001|3880000005376|Personal ID|NEW STRING||15-MAY-2006
2 002|3880000005376|Personal ID|NEW STRING||15-MAY-2007
SQL>
It replaces 4th occurrence of the '[^|]+' pattern with a NEW STRING value.

Select query does not work

Query:
select *
from etm
where emp_id LIKE '009090%'
AND directnumber LIKE '111 123 12345x 67%'
AND cellnumber LIKE '123456789%'
AND phone LIKE '111 123 12345x 67';
database: oracle 11g
Select query doesn't return any records when the LIKE operator has small X character (12345x) in it.
When I replace it with any other character (small/capital) it works ((12345Y)), but replacing it with the small x also does not work.
I cannot modify the query, is there anything can be done at database level while inserting the data?
we are importing data in bulk.
I can't see an issue - here's a test case to demonstrate:
with sample_data as (select '123x 456' str from dual union all
select '123 456' str from dual union all
select '123x' str from dual union all
select 'abcx 93s' str from dual)
select *
from sample_data
where str like '123x%';
STR
--------
123x 456
123x
So, you can see my search has pulled back rows where the str column starts with 123x.
However, if I search for rows starting with 123y then no rows are returned:
with sample_data as (select '123x 456' str from dual union all
select '123 456' str from dual union all
select '123x' str from dual union all
select 'abcx 93s' str from dual)
select *
from sample_data
where str like '123y%';
no rows selected.
since there are no rows where the str column starts with 123y.
Is it a similar case with your data, where there aren't any rows that match all the filter conditions when you have an x in one or more of the like conditions?
I haven't verified this query but it should work.
select *
from etm
where emp_id LIKE '009090%'
AND (directnumber LIKE '111%' or directnumber LIKE '123%' .....)
AND cellnumber LIKE '123456789%'
AND (phone LIKE '111' or phone LIKE '123' .......);
Reference :
https://community.oracle.com/thread/1096523?start=0&tstart=0

SQL Query to show string before a dash

I would like to execute a query that will only show all the string before dash in the particular field.
For example:
Original data: AB-123
After query: AB
You can use substr:
SQL> WITH DATA AS (SELECT 'AB-123' txt FROM dual)
2 SELECT substr(txt, 1, instr(txt, '-') - 1)
3 FROM DATA;
SUBSTR(TXT,1,INSTR(TXT,'-')-1)
------------------------------
AB
or regexp_substr (10g+):
SQL> WITH DATA AS (SELECT 'AB-123' txt FROM dual)
2 SELECT regexp_substr(txt, '^[^-]*')
3 FROM DATA;
REGEXP_SUBSTR(TXT,'^[^-]*')
---------------------------
AB
You can use regexp_replace.
For example
WITH DATA AS (
SELECT 'AB-123' as text FROM dual
UNION ALL
SELECT 'ABC123' as text FROM dual
)
SELECT
regexp_replace(d.text, '-.*$', '') as result
FROM DATA d;
will lead to
WITH DATA AS (
2 SELECT 'AB-123' as text FROM dual
3 UNION ALL
4 SELECT 'ABC123' as text FROM dual
5 )
6 SELECT
7 regexp_replace(d.text, '-.*$', '') as result
8 FROM DATA d;
RESULT
------------------------------------------------------
AB
ABC123
I found this simple
SELECT distinct
regexp_replace(d.pyid, '-.*$', '') as result
FROM schema.table d;
pyID column contains ABC-123, DEF-3454
SQL Result:
ABC
DEF