Frage

I have an sqlite database which has number sequences with random separators. For example

_id  data             
0    123-45/678>90
1    11*11-22-333
2    4-4-5-67891

I want to be able to query the database "intelligently" with and without the separators. For example, both these queries returning _id=0

  SELECT _id FROM myTable WHERE data LIKE '%123-45%'
  SELECT _id FROM myTable WHERE data LIKE '%12345%'

The 1st query works as is, but the 2nd query is the problem. Because the separators appear randomly in the database there are too many combinations to loop through in the search term.

I could create two columns, one with separators and one without, running each query against each column, but the database is huge so I want to avoid this if possible.

Is there some way to structure the 2nd query to achieve this as is ? Something like a regex on each row during the query ? Pseudo code

  SELECT _id 
  FROM myTable 
  WHERE REPLACEALL(data,'(?<=\\d)[-/>*](?=\\d)','') LIKE '%12345%'
War es hilfreich?

Lösung

Ok this is far from being nice, but you could straightforwardly nest the REPLACE function. Example:

SELECT _id FROM myTable
WHERE REPLACE(..... REPLACE(REPLACE(data,'-',''),'_',''), .... '<all other separators>','') = '12345'

When using this in practice (--not that I would recommend it, but at least its simple), you surely might wrap it inside a function.

EDIT: for a small doc on the REPLACE function, see here, for example.

Andere Tipps

If I get it right, is this what you want?

SELECT _id
FROM myTable
WHERE Replace(Replace(Replace(data, '?', ''), '/', ''), '-', '') LIKE '%12345%'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top