In the context of this code challenge, what if we are trying to match on a specific text length?
If you are searching for a match on some specific text length, then instead of utilizing the
% wildcard, which will match 0 or more characters, consider utilizing the
_ wildcard for single character matches.
For example, say that you only remember that some restaurant name had 6 or 7 letters, and started with the letters
"Del". What you can do is utilize the
_ wildcard to match for a specific number of characters, up to a length of 6 or 7.
WHERE name LIKE 'Del___' /* matches "Del" followed by any 3 characters */
OR name LIKE 'Del____'; /* matches "Del" followed by any 4 characters */
That would work in cases where you want to match a pattern that you know is at the beginning or end of a string. Is there a way to do this if you know that the string is 7 characters long and contains the characters “Del” somewhere (but not necessarily at the start or end)?
I figured it out. You can use the AND operator along with LENGTH() which will return length of a string.
SELECT * FROM nomnom WHERE name LIKE ‘%bao%’ AND LENGTH(name) = 7;
When you run the above command in this exercise it will only return one result, however if you remove the condition after the AND, it will return two results.
SELECT * FROM nomnom WHERE name LIKE ‘%bao%’;
The above query will return two results.
In your case, your query would be:
SELECT * FROM nomnom WHERE name LIKE ‘%Del%’ AND LENGTH(name) = 7;
It’s important to note that LENGTH() is a function and not an operator, hence it returns an Integer which is the length of the string passed as an argument.
Great, that makes perfect sense. Thanks for the explanation!
Like riddles, this coding becomes more obvious afterwards, and with practice! Thanks Jephos249