1

I can't figure out the SQL query that would be able to select a row based on a column containing a string but the string could be separated.

For example, the string I am searching for:

1345

Would match:

0123456

That includes 3 things:

1) Any string before

2) Any string after

3) Any string randomly in the middle

I have tried using the following queries

SELECT * FROM Products WHERE FileName LIKE %1345%

SELECT * FROM Products WHERE CONTAINS(FileName, '1345')

But these will not select if the string is randomly in the middle. What should I be doing instead?

1
  • @AvinashRaj is that using LIKE? And also, of course, the string would not be simple numbers so separating the characters like that could be difficult? Commented Feb 15, 2015 at 5:15

2 Answers 2

2

You'd need to pre-prepare for regexp match, so you produce a query like this

SELECT * from name1
WHERE name1.name REGEXP ".*a.*p.*a.*"
Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand how to implement this?
in order to match strings randomly in the middle of your criteria, you need to pre-prepare your criteria BEFORE sending the query using your favorite server side language, you'd append .* before and after each character so REGEXP operator works as expected. Just remember to escape user input tu prevent SQL attacks.
0

This is a working solution tested in MySQL Workbench:

SELECT * FROM Products WHERE FileName LIKE '%1%2%3%';

It matches entries like

2004267695, 2005285881, 20060004841, 20060136398, 20080189084, and 20090189887.

Folio 8 1/2 x 13 in

1992-2003

Comments

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.