3

I use a string for store the days of the week, something like this: MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).

I don't know how to do this in SQL (MySQL).

3 Answers 3

7

use LIKE with %-wildcard between the single characters:

SELECT * FROM table WHERE column LIKE '%M%F%';

note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).

if the characters can be in random order, you'll have to built a query like:

SELECT * FROM table WHERE
  column LIKE '%M%'
AND
  column LIKE '%F%'
[more ANDs per character];
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'

Learn more:

http://www.sqllike.com/

2 Comments

this wont match MWF, but should. the like-clause has to be %M%F% in my opinion.
@oezi: Probably if OP really meant that :)
2

Can you not just say

SELECT * FROM blah WHERE weekday LIKE "%MF%"

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.