SQL like search string starts with

Sql

Sql Problem Overview


Learning SQL. Have a simple table games with field title. I want to search based on title. If I have a game called Age of Empires III: Dynasties, and I use LIKE with parameter Age of Empires III: Dynasties, everything works fine, the search returns the record with that name. But if I search with Age of Empires III, it doesn't return any records:

SELECT * from games WHERE (lower(title) LIKE 'age of empires III');

This doesn't return anything. Should I be using something else instead of LIKE?

I am using MySQL.

Sql Solutions


Solution 1 - Sql

SELECT * from games WHERE (lower(title) LIKE 'age of empires III');

The above query doesn't return any rows because you're looking for 'age of empires III' exact string which doesn't exists in any rows.

So in order to match with this string with different string which has 'age of empires' as substring you need to use '%your string goes here%'

More on mysql string comparision

You need to try this

SELECT * from games WHERE (lower(title) LIKE '%age of empires III%');

In Like '%age of empires III%' this will search for any matching substring in your rows, and it will show in results.

Solution 2 - Sql

You need to use the wildcard % :

SELECT * from games WHERE (lower(title) LIKE 'age of empires III%');

Solution 3 - Sql

Aside from using %, age of empires III to lower case is age of empires iii so your query should be:

select *
from games
where lower(title) like 'age of empires iii%'

Solution 4 - Sql

COLLATE UTF8_GENERAL_CI will work as ignore-case. USE:

SELECT * from games WHERE title COLLATE UTF8_GENERAL_CI LIKE 'age of empires III%';

or

SELECT * from games WHERE LOWER(title) LIKE 'age of empires III%';

Solution 5 - Sql

If you used "like" operator without %, the like work as the equal (=), that's mean your code will be as this:

select * from games where lower(title)= 'age of empires iii';

So it doesn't find any matched data then nothing returned.

But if you want to compare only part of the title with your parameter, you should use % sign. You can use it in different positions and this depends on the logic of the database.

Usually it used in the begin and end of the parameter, and the code be like this:

select * from games where lower(title) like '%age of empires iii%';

This will search about the value in the title column even if there is a non-match characters in the beginning or in the end or both. The important condition it will search about is that the value send as argument to the like operator (in this case the value is "age of empires iii") found somewhere in the title field (in this case the title is "Age of Empires III: Dynasties"), and this condition is true, so this row and and any other row with same case will be returned.

Solution 6 - Sql

In SQL %---% matches any character in between. select * from table_name where column_name line '%kk%'; will match any string that includes 'ss' for instance:

  • llssll
  • ssll If you want to match the start of the string you should use 'ss%' instead. select * from table_name where column_name line 'ss%'; this query will return only ssll.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question0xSinaView Question on Stackoverflow
Solution 1 - SqlVishwanath DalviView Answer on Stackoverflow
Solution 2 - SqlmishaView Answer on Stackoverflow
Solution 3 - Sqlmatepal297View Answer on Stackoverflow
Solution 4 - SqlDhwaniView Answer on Stackoverflow
Solution 5 - SqlalnajmView Answer on Stackoverflow
Solution 6 - SqlKaan AteşelView Answer on Stackoverflow