Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL find number inside a Comma seperated string

I'm trying to find a number (ID) inside Comma seperated string. I tried it with a Select but didn't work. Is there another Method I could use?

SELECT CASE WHEN TO_CHAR(1) IN ('1, 2, 3, 4') THEN 1 ELSE 0 END AS Result
FROM DUAL

I always get '0' . Can anyone please help here.

like image 776
TheNewby Avatar asked May 08 '26 09:05

TheNewby


1 Answers

Such query can convert for you comma separated list into table:

select regexp_substr('1,2,3,4','[^,]+', 1, level) val from dual
  connect by regexp_substr('1,2,3,4', '[^,]+', 1, level) is not null;

Then you can perform in on result of the query.

Another nice option is:

select to_number(column_value) as IDs from xmltable('1,2,3,4,5');
like image 71
Kacper Avatar answered May 11 '26 11:05

Kacper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!