0

I've come across a search situation that I am unsure how to approach. I am searching through a set of a column which is a string for example

Row 1: Column Content: 7;15;25
Row 2: Column Content: 5
Row 3: Column Content: 5;7

but I would like the row that has only 5

example: Select * from table where column like '%5%'

The problem with this case is that, the query will bring back all rows when I only needed Row 2 and Row 3

Is there anything that I could do with the query that will allow me to bring back the desired results.

I would truly appreciate the assistance. If there is any confusion with the question, I'll be happy clarify.

0

1 Answer 1

1

How about:

SELECT * FROM MyTable WHERE
CONTENT LIKE '5;%'      -- Starts with 5
OR CONTENT LIKE '%;5;%' -- 5 in the middle somewhere
OR CONTENT LIKE '%;5'   -- Ends with 5
OR CONTENT = '5';       -- 5 is the only item in the list

You could also simplify it as:

SELECT * FROM MyTable WHERE concat(';',CONTENT,';') LIKE '%;5;%';

Both of these solutions will probably result in fairly poor performance, especially if your table is huge. To solve that, I'd recommend normalizing this data and storing each selection as a row in another table.

Sign up to request clarification or add additional context in comments.

1 Comment

The first one worked best with the table i am using. I agree with you about the normalizing part. lol. Thank you so much. :)

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.