savagehax
savagehax

Reputation: 55

sqlite3: How to query for results where a column contains a certain substring

How do I select the rows of a sqlite3 database where a TEXT column contains a desired substring that I am searching for? I am trying to do this in python3.

The value of the column may vary across the many rows, and will not always match the substring exactly; however, the value of the column may contain the substring.

Upvotes: 2

Views: 475

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20919

Sounds like you need the LIKE operator.

It uses _ as a single-character wildcard and % as an any-length wildcard.
For example: WHERE col LIKE '%foo%' will match return any rows where col contains the text foo.
Note that LIKE is case-sensitive by default. If you need to make it case-insensitive, apply UPPER to the searched column and the search pattern: WHERE UPPER(col) LIKE UPPER('%foo%')

Upvotes: 1

Related Questions