Check if all characters are 'X' - sql

I have the below table:
COL
---
XXY
YXX
XXX
NULL
I want to filter out the rows which don't consist of all 'X's.
Expected output:
COL
---
XXX

We can use REGEXP_LIKE here:
SELECT COL
FROM yourTable
WHERE REGEXP_LIKE(COL, '^X+$'); -- ^X+$ means all X from start to end
Another similar version:
SELECT COL
FROM yourTable
WHERE NOT REGEXP_LIKE(COL, '[^X]'); -- this means no non X present

Another option(without using a regular expression) might be using
WITH t(col) AS
(
SELECT 'XXY' FROM dual UNION ALL
SELECT 'YXX' FROM dual UNION ALL
SELECT 'XXX' FROM dual UNION ALL
SELECT NULL FROM dual UNION ALL
SELECT 'XX ' FROM dual
)
SELECT *
FROM t
WHERE REPLACE(NVL(col,'Y'),'X') IS NULL;
COL
----
XXX
without forgetting the case col = NULL through use of a NVL()

You can use the following syntax (assuming you are using MySQL database 5.6 or greater version):
SELECT * FROM table_name WHERE col_name REGEXP '^X+$';

If you don't want/have regexp then:
WITH
tbl AS
( Select 'XXY' "COL" From dual Union All
Select 'YXX' "COL" From dual Union All
Select 'XXX' "COL" From dual Union All
Select null "COL" From dual
)
Select COL
From tbl
Where Length(Nvl(COL, 'Z')) - Length( Replace( Upper(Nvl(COL, 'Z')), 'X', '')) Is Null
COL
---
XXX
This covers both small 'x' and capital 'X' if needed and returns original COL value

Related

Find value that is not a number or a predefined string

I have to test a column of a sql table for invalid values and for NULL.
Valid values are: Any number and the string 'n.v.' (with and without the dots and in every possible combination as listed in my sql command)
So far, I've tried this:
select count(*)
from table1
where column1 is null
or not REGEXP_LIKE(column1, '^[0-9,nv,Nv,nV,NV,n.v,N.v,n.V,N.V]+$');
The regular expression also matches the single character values 'n','N','v','V' (with and without a following dot). This shouldn't be the case, because I only want the exact character combinations as written in the sql command to be matched. I guess the problem has to do with using REGEXP_LIKE. Any ideas?
I guess this regexp will work:
NOT REGEXP_LIKE(column1, '^([0-9]+|n\.?v\.?)$', 'i')
Note that , is not a separator, . means any character, \. means the dot character itself and 'i' flag could be used to ignore case instead of hard coding all combinations of upper and lower case characters.
No need to use regexp (performance will increase by large data) - plain old TRANSLATE is good enough for your validation.
Note that the first translate(column1,'x0123456789','x') remove all numeric charcters from the string, so if you end with nullthe string is OK.
The second translate(lower(column1),'x.','x') removes all dots from the lowered string so you expect the result nv.
To avoid cases as n.....v.... you also limit the string length.
select
column1,
case when
translate(column1,'x0123456789','x') is null or /* numeric string */
translate(lower(column1),'x.','x') = 'nv' and length(column1) <= 4 then 'OK'
end as status
from table1
COLUMN1 STATUS
--------- ------
1010101 OK
1012828n
1012828nv
n.....v....
n.V OK
Test data
create table table1 as
select '1010101' column1 from dual union all -- OK numbers
select '1012828n' from dual union all -- invalid
select '1012828nv' from dual union all -- invalid
select 'n.....v....' from dual union all -- invalid
select 'n.V' from dual; -- OK nv
You can use:
select count(*)
from table1
WHERE TRANSLATE(column1, ' 0123456789', ' ') IS NULL
OR LOWER(column1) IN ('nv', 'n.v', 'nv.', 'n.v.');
Which, for the sample data:
CREATE TABLE table1 (column1) AS
SELECT '12345' FROM DUAL UNION ALL
SELECT 'nv' FROM DUAL UNION ALL
SELECT 'NV' FROM DUAL UNION ALL
SELECT 'nV' FROM DUAL UNION ALL
SELECT 'n.V.' FROM DUAL UNION ALL
SELECT '...................n.V.....................' FROM DUAL UNION ALL
SELECT '..nV' FROM DUAL UNION ALL
SELECT 'n..V' FROM DUAL UNION ALL
SELECT 'nV..' FROM DUAL UNION ALL
SELECT 'xyz' FROM DUAL UNION ALL
SELECT '123nv' FROM DUAL;
Outputs:
COUNT(*)
5
or, if you want any quantity of . then:
select count(*)
from table1
WHERE TRANSLATE(column1, ' 0123456789', ' ') IS NULL
OR REPLACE(LOWER(column1), '.') = 'nv';
Which outputs:
COUNT(*)
9
db<>fiddle here

Oracle SQL REGEXP for finding a value between columns, irrespective of their position

I have following set of data in two different columns in a table. I need to check if the BB% value is matching between both the columns or not.
Column A
Column B
BB12,AA13,CC24
AA13,BB12,CC24
BB99,AA34,CC78
AA34,CC78,BB77
AA22,BB33,CC77
AA22,BB33,BB44,CC77
I initially tried using below REGEXP. But this will only work if the BB value in column A is always in position 1 and in Column B with position 2. But I need a REGEXP to check something for row 2 and row 3 scenarios.
REGEXP_SUBSTR(ColumnA, '\w+', 1, 1) <> REGEXP_SUBSTR(ColumnB, '\w+', 1, 2)
You can try this. But, it does not take account the case there are more than one occurrence of BBXXX in the columnA. In such a case, the comparison will only be based on the first occurence of BBXXX in the columnA.
with your_table (ColumnA, ColumnB) as (
select 'BB12,AA13,CC24', 'AA13,BB12,CC24' from dual union all
select 'BB99,AA34,CC78', 'AA34,CC78,BB77' from dual union all
select 'AA22,BB33,CC77', 'AA22,BB33,BB44,CC77' from dual union all
select 'AA22,BB33,CC77', 'AA22,BB44,BB33,CC77' from dual union all
select 'AA22,BB33,CC77', 'BB33,AA22,BB44,CC77' from dual
)
select COLUMNA, COLUMNB
from your_table t
where ','||COLUMNB||',' NOT like '%,'||regexp_substr(COLUMNA, 'BB[^,]*', 1, 1)||',%'
;
I believe this will do the trick and allow for the value in COLB to be in any position. Return the rows where the first "BB" element in COLA is not in COLB. Note row 3 has the "BB" values swapped from the original example to show order does not matter.
WITH tbl(ID, cola, colb) AS (
SELECT 1, 'BB12,AA13,CC24','AA13,BB12,CC24' FROM dual UNION ALL
SELECT 2, 'BB99,AA34,CC78','AA34,CC78,BB77' FROM dual UNION ALL
SELECT 3, 'AA22,BB33,CC77','AA22,BB44,BB33,CC77' FROM dual
)
SELECT ID, COLA, COLB
FROM tbl T
WHERE NOT REGEXP_LIKE(COLB, REGEXP_SUBSTR(COLA, 'BB[^,]*'));
ID COLA COLB
---------- -------------- -------------------
2 BB99,AA34,CC78 AA34,CC78,BB77
I suggest extracting the BBxx substring from each string and then compare the two for equality or inequality:
WITH cteData(ColumnA,ColumnB) AS
(SELECT 'BB12,AA13,CC24', 'AA13,BB12,CC24' FROM DUAL UNION ALL
SELECT 'BB99,AA34,CC78', 'AA34,CC78,BB77' FROM DUAL UNION ALL
SELECT 'AA22,BB33,CC77', 'AA22,BB33,BB44,CC77' FROM DUAL)
SELECT COLUMNA,
REGEXP_SUBSTR(COLUMNA, 'BB[^,]+') AS COLA_BB,
COLUMNB,
REGEXP_SUBSTR(COLUMNB, 'BB[^,]+') AS COLB_BB
FROM cteData
WHERE REGEXP_SUBSTR(COLUMNA, 'BB[^,]+') = REGEXP_SUBSTR(COLUMNB, 'BB[^,]+')
dbfiddle here

regexp match character before a specify string (not include)

REGEXP_SUBSTR(label) ,'.*_dis')
this is for sql;
my database is mysql
select REGEXP_SUBSTR(label) ,'.*_dis') as dis ,
substr(label,length(label))-1) as num
from table
table.lable col's data:
1. a_b_dis_12
2. a_dis_13
3. c_d_dis_23
3. c_dis_22
i want to get the character before '_dis' and the numeric part use regexp
1.a_b 12
2.a 13
3.c_d 23
4.c 22
thanks a lot!
You can use regexp_substr as follows:
Select regexp_substr(your_column, '^(.*)_dis_[0-9]+$',1,1,null,1) as dis,
Regexp_substr(your_column, '[0-9]+$') as num
From your table
You can use regexp_replace():
select regexp_replace(col, '^(.*)_dis.*$', '\1'),
regexp_replace(col, '^.*_dis_([0-9]+)', '\1')
from (select 'a_b_dis_12' as col from dual union all
select 'a_dis_13' as col from dual union all
select 'c_d_dis_23' as col from dual union all
select 'c_dis_22' as col from dual
) t;
Here is a db<>fiddle.
I would use regexp_replace() as follows:
select
regexp_replace(label, '_dis_.*$', '') dis,
regexp_replace(label, '^.*_dis_', '') num
from mytable
The first expression suppresses everything from '_dis_ (included) to the end of the string. The second expression removes everything from the beginning of the string until '_dis_' (included).

Comparing 2 lists in Oracle

I have 2 lists which I need to compare. I need to find if at least one element from List A is found in List B. I know IN doesn't work with 2 lists. What are my other options?
Basically something like this :
SELECT
CASE WHEN ('A','B','C') IN ('A','Z','H') THEN 1 ELSE 0 END "FOUND"
FROM DUAL
Would appreciate any help!
You are probably looking for something like this. The WITH clause is there just to simulate your "lists" (whatever you mean by that); they are not really part of the solution. The query you need is just the last three lines (plus the semicolon at the end).
with
first_list (str) as (
select 'A' from dual union all
select 'B' from dual union all
select 'C' from dual
),
second_list(str) as (
select 'A' from dual union all
select 'Z' from dual union all
select 'H' from dual
)
select case when exists (select * from first_list f join second_list s
on f.str = s.str) then 1 else 0 end as found
from dual
;
FOUND
----------
1
In Oracle you can do:
select
count(*) as total_matches
from table(sys.ODCIVarchar2List('A', 'B', 'C')) x,
table(sys.ODCIVarchar2List('A', 'Z', 'H')) y
where x.column_value = y.column_value;
You need to repeat the conditions:
SELECT (CASE WHEN 'A' IN ('A', 'Z', 'H') OR
'B' IN ('A', 'Z', 'H') OR
'C' IN ('A', 'Z', 'H')
THEN 1 ELSE 0
END) as "FOUND"
FROM DUAL
If you are working with collection of String you can try Multiset Operators.
create type coll_of_varchar2 is table of varchar2(4000);
and:
-- check if exits
select * from dual where cardinality (coll_of_varchar2('A','B','C') multiset intersect coll_of_varchar2('A','Z','H')) > 0;
-- list of maching elments
select * from table(coll_of_varchar2('A','B','C') multiset intersect coll_of_varchar2('A','Z','H'));
Additionally:
-- union of elemtns
select * from table(coll_of_varchar2('A','B','C') multiset union distinct coll_of_varchar2('A','Z','H'));
select * from table(coll_of_varchar2('A','B','C') multiset union all coll_of_varchar2('A','Z','H'));
-- eelemnt from col1 not in col2
select * from table(coll_of_varchar2('A','A','B','C') multiset except all coll_of_varchar2('A','Z','H'));
select * from table(coll_of_varchar2('A','A','B','C') multiset except distinct coll_of_varchar2('A','Z','H'));
-- check if col1 is subset col2
select * from dual where coll_of_varchar2('B','A') submultiset coll_of_varchar2('A','Z','H','B');
I am trying to do something very similar but the first list is another field on the same query created with listagg and containing integer numbers like:
LISTAGG(my_first_list,', ') WITHIN GROUP(
ORDER BY
my_id
) my_first_list
and return this with all the other fields that I am already returning
SELECT
CASE WHEN my_first_list IN ('1,2,3') THEN 1 ELSE 0 END "FOUND"
FROM DUAL

How to convert String in SQL (ORACLE)

I try to select from table_1 where ITEM_FIELD_A is not in ITEM_FIELD_B. The Item_FIELD_B value are look as below. I was expecting no COVER_TAPE & SHIPPING_REELS will be selected. But unfortunately, it's not working.
The sql I used to select the table
select * from table_1 where MST.ITEM_FIELD_A not in ITEM_FIELD_B
Question:
In Oracle, is there any function to decode the string. so that the above select statement will not return COVER_TAPE and SHIPPING_REELS??
The IN operator would be used when you wish to compare (or negate) one item in a list such as
WHERE ITEM_FIELD_A NOT IN ('COVER_TAPE', 'SHIPPING_REELS', '')
What you want is the LIKE operator:
WHERE ITEM_FIELD_B NOT LIKE '%' || ITEM_FIELD_A || '%'
Apologies if I got the wildcard wrong, been a while since I last touched Oracle.
Check out below Query:
WITH TAB1 AS
( SELECT 'COVER_TAPE' ITEM_A FROM DUAL
UNION
SELECT 'CARRIER_TAPE' ITEM_A FROM DUAL
UNION
SELECT 'SHIPPING_REELS' ITEM_A FROM DUAL
),
TAB2 AS
(
SELECT 'COVER_TAPE,SHIPPING_REELS' ITEM_B FROM DUAL
)
SELECT ITEM_A, ITEM_B FROM TAB1, TAB2 WHERE INSTR(ITEM_B, ITEM_A) <=0
INSTR will return >0 if same sequence of characters is available.
SQL> with t(x , y) as
2 (
3 select 'A', q'[('A','B','C')]' from dual union all
4 select 'R', q'[('A','B','C','D')]' from dual union all
5 select 'C', q'[('A', 'C','D')]' from dual
6 )
7 select x, y
8 from t where y not like q'[%']'||x||q'['%]'
9 /
X Y
---------- --------------------------------------------------
R ('A','B','C','D')