1

I've a field that looks like this

1,13,15

If I try to make a search to find all rows than contains "1", then it's taking all rows that have 1 inside, and not only rows that says 1, [something], but also "11","13" etc.

I've tried with a like statement and wildcards, but without luck.

Ilyes
  • 14,367
  • 4
  • 24
  • 50
Simon Thomsen
  • 1,321
  • 7
  • 27
  • 36

7 Answers7

3

If you're using MySQL, use FIND_IN_SET, not LIKE.

WHERE FIND_IN_SET('1', columnname)

But the best solution is to normalize your schema so you don't have comma-separated values in a column.

Barmar
  • 669,327
  • 51
  • 454
  • 560
1

If you are using MySQL you can use regexp to check such values

where column_name regexp '^1,|,1,|,1$|^1$'
Vamsi Prabhala
  • 47,581
  • 4
  • 34
  • 53
1

When you say "1*" it finds everything that has a one and anything after that. Just narrow down your search and serach for:

 field LIKE "1,%" OR field LIKE "%,1,%" OR field LIKE "%,1" OR field = "1"
Amir Ziarati
  • 13,390
  • 11
  • 42
  • 50
0

You can search for "1," OR "1" OR ", 1".

vctrd
  • 444
  • 3
  • 8
0

if your field is '1,13,15' change it to ',1,13,15,'

and your search to LIKE '%,1,%'

So depending on your db you should try something like this

 SELECT  * 
 FROM yourTable
 WHERE ','  + yourField + ',' LIKE  '%,' + @search + ',%'
Juan Carlos Oropeza
  • 45,789
  • 11
  • 74
  • 113
0

Have you tried:

select rows
from table
where field contains '1,'

?

Nisse Engström
  • 4,636
  • 22
  • 26
  • 40
0

If you're using MS SQL Server, you should use LIKE '%1%'.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
jjones150
  • 171
  • 2
  • 13