I am trying to retrieve information from my database.
I have words like Lim Won Mong
and Limited
.
If I do my query, SELECT name FROM memberdb WHERE name LIKE '%LIM%'
, it displays both Lim Won Mong
and Limited
and I only want data from Lim Won Mong
.
How should I rewrite my query?
If you want to match the word "lim" (so it matches "a lim", "lim a", "a lim a", and "lim", but not "limit" or "alim"), you can use the SQL
REGEXP
keyword, and use word boundaries ([[:<:]]
and[[:>:]]
). These match whitespace (spaces, tabs, newlines), string start/end, and some punctuation, as well.Something like this:
Note that
REGEXP
is NOT case-sensitive (unless using binary strings).If you want it to match ONLY spaces, you can still use
REGEXP
; this will match either the start of the string or a space, then "lim", then a space or the end of the string:Both solutions have been tested on SQL versions which support
REGEXP
(SQLite 3+; all of Android uses 3.5.9 or higher).Try this one:
Suppose you have data something like this:
This query will return you only following data:
See this SQLFiddle
Execute following Query: