[SELECT] Simple way to search for data that start with given pattern?
Hello, I need to find all the records where the ZIP code starts with such and such digits. Apparently, SQLite doesn't support regexes ("Error: no such function: REGEXP"), so that won't do: SELECT COUNT(*) FROM MyTable WHERE zip REGEXP '^(01|04|54)'; SELECT COUNT(*) FROM MyTable WHERE zip REGEXP '^01|^04|^54'); Is there something easier than a long list of LIKE? SELECT COUNT(*) FROM MyTable WHERE zip LIKE "01%" OR LIKE "04%" etc. Thank you.
You can indeed just load the REGEXP function. One such implementation of the regexp function can be found here:
And your syntax is incorrect.
SELECT COUNT(*) FROM MyTable WHERE zip LIKE "01%" OR LIKE "04%" etc.
select count(*) from mytable where zip like '01%' or zip like '04%' etc.
double quotes are for identifiers. single quotes are for strings.
Unlike English (or COBOL) you cannot elide the subject of the likeness.
This is not really answering the question about "something easier" to achieve yoru goal (because as Keith pointed out, there is no "something easier", though you can load the REGEX function itself and use that).
However, in the specific query you are trying to achieve, the most concise way to put it (barring loading the REGEX function), would be:
SELECT COUNT(*) FROM MyTable WHERE SUBSTR(zip,1,2) IN ('01','04','54');
The problem with this approach (and the REGEX approach, unless there's an optimization I'm unaware of) is that they will be slower (assuming the "zip" column is indexed) than "... LIKE '01%' OR LIKE '04%' OR ..." because LIKE has an optimization whereby, if the searched-for entity is of the form "xxx%" (as opposed to '%xxx' or '%xx%') then it can use the Index, resulting in a much faster lookup.
Presumably if one were to create an index on the expression SUBSTR(zip,1,2) then the planner would put the temporary table for the IN list in the outer loop and descend into MyTable using that index.
LIKE would, of course, only use a case-insensitive index on zip unless it were told that like were case_sensitive -- the fact that zip contains numbers only is quite irrelevant to whether a text field is case sensitive or not.
Maybe incorporate GLOB() in some way
There are a number of different ways this problem can be solved, but they each have costs: programming time, storage space, etc.. To help the people trying to give you an answer suited to your application, can you tell us
(A) How many rows you have in MyTable ? Is it more like thousands or millions or billions ?
(B) Is it important in your app that answers can be returned in less than a second, or is a delay of a couple of seconds acceptable, or is a longer delay acceptable ?