Home > database >  particular column has multiple data separated with comma, how to write a query to return that partic
particular column has multiple data separated with comma, how to write a query to return that partic

Time:05-13

For example

col1 col2
1 2,3,4

Referring to the table shown here, if I want to return a row if col1=1 and col2 has 2,3.

How to write query for this scenario?

Bit of a silly question but I am new to SQL and need your help!

CodePudding user response:

You can just use LIKE to detect the comma in column2

SELECT * FROM <table> WHERE COLUMN2 LIKE '%,%'

CodePudding user response:

You can use the string_to_array function to convert your string to an array, then use the ANY in your condition to get the desired strings.

select * from  my_table where 3 = ANY (string_to_array(col2,',')::int[])

Demo in DBfiddle

  • Related