-1
SELECT name
FROM TV
WHERE name LIKE '%Day%'
AND name NOT LIKE '%Days%'
            AND name NOT LIKE '%Hayday%'
            AND name NOT LIKE '%Payday%'
            AND name NOT LIKE '%Doomsday%'

How could I merge the last 3 queries so that the word 'day' is not included in any word except for a word on its own?

Thaid
  • 1
  • 1

5 Answers5

3

Spaces are a part of a string... assuming that you're searching inside a phrase, Or at the beginning or end of it. This is a simple case solution, assuming no punctuations, just texts and spaces. then a solution should be something like:

SELECT name
FROM Track
WHERE name (name  LIKE '% Day %') OR (name  LIKE '% Day') OR (name  LIKE 'Day %')
;
Guy Louzon
  • 1,175
  • 9
  • 19
3

If you are using MySQL, you can use RLIKE (REGEXP) to match only against the word by itself by creating an expression with word boundaries:

SELECT name
FROM TV
WHERE name REGEXP '[[:<:]]Day[[:>:]]'

Note that REGEXP is case-insenstive so this will also match the word "day".

If you are using SQLite, it is possible you have REGEXP installed in which case this solution should work for you too. If not, this question gives guidance as to how you may be able to install it.

Nick
  • 138,499
  • 22
  • 57
  • 95
0

In your query, instead of 'Like' keyword, you can directly give the string like "where name ='time'

karthi keyan
  • 53
  • 2
  • 7
0

You can by using FIND_IN_SET

SELECT name
FROM Track
WHERE name LIKE '%Time%'
AND FIND_IN_SET(name,"Times,Playtime,Summertime,Wartime")<0
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Assuming by "on its own" you mean surrounded by spaces, then the simplest method might be:

SELECT name
FROM TV
WHERE ' ' || name || ' ' LIKE '% Day %';

(Or however your database does string concatenation.)

If "on its own" simply means a word boundary, then regular expressions are the best method.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786