3

I have a column that has multiple numbers separated by a comma. Example for a row:

`numbers`:
 1,2,6,66,4,9

I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.

I cant use LIKE because if there is 66 it'll work too.

HTMHell
  • 5,021
  • 5
  • 34
  • 75

2 Answers2

7

You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:

where ','+numbers+',' like '%,'+'6'+',%'

SQL Server uses + for string concatenation. Other databases use || or the concat() function.

Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
1

You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:

id   numbers
 1   1,2,6,66,4,9

You would have a new table that joins those values like so

row_id   number
1        1
1        2
1        6
1        66
1        4
1        9

Then you can search for the number 6 in the number column and get the row_id

juuga
  • 1,244
  • 1
  • 13
  • 24