Your initial attempt will only match countries that have three adjacent a
s in the name. Try the following:
SELECT name FROM world
WHERE name LIKE '%a%a%a%'
The character a
matches only the character a
(case sensitivity depends on the table and server, for MS SQL at least). The character %
will match any number of characters, any number of times. The seven characters in my query will mean that there are at most seven groups. For example:
Afghanistan
is broken into
, A
, fgh
,a
,nist
,a
, and n
.
Algeria
is broken into
, A
, lgeri
, a
,
, and then there is no remaining a
to include, so it is not included as a match.
Madagascar
could be broken into Mad
, a
, g
, a
, sc
, a
, and r
. Note that the first group contains an a
, because %
allows any character, including a
. (Here I assume the pattern matching is greedy, which means it makes the capture groups as large as possible, which prefers earlier groups to later groups.)