简体   繁体   中英

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

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.

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.

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')         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM