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,640
  • 4
  • 29
  • 55
Simon Thomsen
  • 1,351
  • 7
  • 27
  • 37

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
  • 741,623
  • 53
  • 500
  • 612
1

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

where column_name regexp '^1,|,1,|,1$|^1$'
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
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
  • 14,248
  • 11
  • 47
  • 52
0

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

vctrd
  • 488
  • 4
  • 9
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
  • 47,252
  • 12
  • 78
  • 118
0

Have you tried:

select rows
from table
where field contains '1,'

?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
0

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jjones150
  • 168
  • 2
  • 14