0

How to filter an SQL table on the basis of a VALUE that exist in its CSV column. In detail, I have a table that has a CSV column containing integer number in comma separated form like

12,234,32,55 
121,64,43,65
54,25,112,12
996,612,55,3  etc.

now from this table I want to find out the rows that has the value 12(twelve) in csv Column i.e., the row with csv record

12,234,32,55
54,25,112,12 only

Please Help

1
  • It would be better to redesign your schema so that each value was stored in a separate row. You're not meant to stuff multiple values into a single column. Commented Feb 7, 2013 at 7:20

1 Answer 1

1
Select *
from Tablex 
where 
(Col like '%,12,%') 
 or (Col = '12')
 or LEFT(Col,3)='12,'
 or RIGHT(col,3)=',12'

or better as Love2Learn suggested

 Select * From Tablex Where ','+Col+',' Like '%,12,%' 
Sign up to request clarification or add additional context in comments.

4 Comments

You can do this to simplify it... Select * From Tablex Where ','+Col+',' Like '%,12,%'
Thanks @Love2Learn, it works perfectly... Can u please post it as answer, so i can accept it... :-)
Also thanks @bummi for your answer... it works too but a bit difficult to understand for me
@bummi answer is more or less the same. Bummi, can you update your answer to the shortened form so OP can accept your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.