0

I know the title is not good i don't know how to explain that.

I have table like

Id    AvaibleSports
1     [1]
2     [1]
3     [2],[10]
4     [4],[10]

I know this is not the best way to store information but this is all i have right now. I need to filter rows based on avaible sports value.

For example when i filter for "1" i must get records 1 and 2. When i filter for "10", i need to get 3 and 4 back.

I tried to filter with like statement like this:

select Id, AvaibleSports FROM myTable wHERE AvaibleSports like '%[10]%'

but this is also returning records containing [1].

I also tried to filter like

select Id, AvaibleSports FROM myTable wHERE AvaibleSports like '%10%'

This is returning correct result for 10 but wrong result for 1.

How can i filter this correctly?

Sefa
  • 8,865
  • 10
  • 51
  • 82

1 Answers1

1

you can alternately use CHARINDEX . Something like this.

DECLARE @table1 TABLE
(Id INT IDENTITY(1,1),
AvaibleSports VARCHAR(10)
)
INSERT INTO @table1(AvaibleSports)
SELECT '[1]' a
UNION ALL SELECT '[1]'
UNION ALL SELECT '[2],[10]'
UNION ALL SELECT '[4],[10]'

SELECT * FROM @table1 WHERE CHARINDEX('[10]',AvaibleSports) > 0
ughai
  • 9,830
  • 3
  • 29
  • 47