173

How do I do this?

For example, if my column is "cats,dogs,birds" and I want to get any rows where column contains cats?

2
  • 5
    edit your question with a set of sample data. It's hard to determine exactly what you're asking for. Commented Aug 17, 2010 at 2:35
  • Seems like you can also use the CONTAINS command but its implementation doesn't appear to be standardized and I couldn't get it to work using SQLite; however, there is documentation for it at [Oracle][1] and [Microsoft][2] and it was supposedly faster according to another [thread][3] but you may need to use another type of SQL instead of SQLite Commented Jan 20, 2021 at 21:28

2 Answers 2

279

Using LIKE:

SELECT *
  FROM TABLE
 WHERE column LIKE '%cats%'  --case-insensitive
Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind that this isn't suitable where the string you're searching for is variable or contains a special character such as %.
if "cats" is a field from an other table, you could use '%'||table2.field||'%'as the like criteria
140

While LIKE is suitable for this case, a more general purpose solution is to use instr, which doesn't require characters in the search string to be escaped. Note: instr is available starting from Sqlite 3.7.15.

SELECT *
  FROM TABLE  
 WHERE instr(column, 'cats') > 0;

Also, keep in mind that LIKE is case-insensitive, whereas instr is case-sensitive.

2 Comments

Which means that your minSdkVersion should be 21 (5.0-Lollipop)
@WilliamEntriken In my own test, instr(...) is a little bit faster (0.32s vs 0.34s). You can use .timer on in SQLite to measure the execution time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.