SQLite Like: Querying Data Based On Pattern Matching
Summary: in this tutorial, you will learn how to query data based on pattern matching using SQLite LIKE
operator.
Introduction to SQLite LIKE
operator
Sometimes, you don’t know exactly the complete keyword that you want to query. For example, you may know that your most favorite song contains the word,elevator
but you don’t know exactly the name.
To query data based on partial information, you use the SQLite LIKE operator in the WHERE clause of the SELECT statement as follows:
Note that you can also use the LIKE
operator in the WHERE
clause of other statements such as DELETEand UPDATE.
There are two ways to construct a pattern using percent sign % and underscore _
wildcards:
- The percent sign
%
wildcard matches any sequence of zero or more characters. - The underscore
_
wildcard matches any single character.
The percent sign % wildcard examples
The percent sign s%
matches any string that starts with s
e.g.,son
so
, etc. The %er
pattern matches any string that ends with er
e.g., peter
, clever
, etc. And the %per%
pattern matches any string that contains per
such as percent
, peeper
, etc.
SELECT HHH from NANDU WHERE HHH LIKE '%brah%'
to search for HHH values containing brah anywhere within them.
(I chose this pattern because it would catch both Ibrahim and Abraham)
The % character is the match against anything. If you want to just
do prefixes, just use the trailing %.
I don't see the advantage in using a bind parameter in a dynamic environment like this.
Use the AI2 text JOIN block to build up the WHERE clause:
JOIN(
HHH LIKE '
textbox.Text
%'
)
This is a 3 way JOIN block, and the ' marks are necessary for the SQL generation.
ABG