0
I need to sort a query like the example below, where, the searched text is 'un', it first brings the items that start with 'un'. This is exactly what I need, to order the output of the query, so that the items that start with the text are the first of the result, but without eliminating the others.
Using this answer from Stackoverflow: https://stackoverflow.com/a/1588817/1997073
I created a query in Sqlite like this:
SELECT ProDescricao FROM ProdutosDB
where ProDescricao like '%un%'
ORDER BY CASE WHEN ProDescricao like 'un %' THEN 0
WHEN ProDescricao like 'un%' THEN 1
WHEN ProDescricao like '%un%' THEN 2
ELSE 3
END, ProDescricao;
Output example:
28 |UNIAO SOLD 40--
29 |UNIAO SOLD 50--
30 |UNIAO SOLD 60--
35 |ADAPT UNIVERSAL
44 |ADESIVO JUNTA 3M
48 |ALIC MINI 4,5 UN
34 |ACESS WC JUNIOR
My question is, how to do this, with parameter? Calling the query by Android using Sqlitedatabase Query?
Note that the parameter would always be the same:
SELECT ProDescricao FROM ProdutosDB
where ProDescricao like '%'+@busca+'%'
ORDER BY CASE WHEN ProDescricao like ''+@busca+' %' THEN 0
WHEN ProDescricao like ''+@busca+'%' THEN 1
WHEN ProDescricao like '%'+@busca+'%' THEN 2
ELSE 3
END, ProDescricao;
Is it something like that? Is it correct to use the Sqlitedatabase Query or should I use some other way?
Thanks in advance
In fact the way I posted it doesn’t work because the
?
is replaced by the value between plicas or would look like this%'valor'%
what is not SQL valid.– ramaral