SBK
SBK

Reputation: 81

Search with text not in order, in an SQL database

If I have a column storing a string that is a concatenation of different days, say "Monday, Wednesday, Saturday, Sunday", is there a command in SQL, which would return that string, by only searching for "Wednesday, Sunday"?

Something along the lines of the command below:

    SELECT * FROM `information` WHERE Days = "Wednesday, Sunday"

Upvotes: 1

Views: 574

Answers (4)

soqui
soqui

Reputation: 3

You could use the LIKE command

SELECT * FROM `information` WHERE Days LIKE "%Wednesday% AND Days LIKE '%Sunday%";

Upvotes: -1

Aishwarya
Aishwarya

Reputation: 433

You can Use Regular expressions.It supported in all commonly used DB engines.

In MySql there is RLIKE Operator, so your query would be something like:

 SELECT * FROM `information` WHERE Days RLIKE '(Wednesday|Sunday)+.*(Wednesday|Sunday)+';

Upvotes: 1

jimmu
jimmu

Reputation: 1056

If the list of days is a string

SELECT * FROM `information` WHERE Days LIKE '%Wednesday%' AND Days LIKE '%Sunday%'

EDIT Changed OR to AND to get rows having both dates

Upvotes: 2

NetMage
NetMage

Reputation: 26927

Convert your source query into a like query, assuming the search string is in the right order:

SELECT *
FROM information
WHERE Days LIKE CONCAT('%', REPLACE(REPLACE('Wednesday, Saturday', ', ', '%'), ',', '%')), '%')

The expression converts

Wednesday, Saturday

to

%Wednesday%Saturday%

Upvotes: 0

Related Questions