How can I sort the characters in a string value in SQLite Database? For example:
"macedonia" --sorted--> "aacdeimno"?
I know one method: if I use another table: integers(i int) (1,2,3,4,,,,,100) and make this query:
SELECT group_concat(l ,'' )
FROM
( SELECT substr('macedonia',i,1) as l
from integers
where i BETWEEN 1 and length('macedonia')
ORDER BY l
)
-- SORT WORKS ! --> 'macedonia'---> 'aacdeimno'
But this method is too complex if I use it inside other complex queries ! Howto pass string 'macedonia' as parameter of a query ? In MS Access there are so called PARAMETER QUERIES ...
Do you know something more simple ? Maybe using CTE, or REGEXP ?? For PostgreSQL database I wrote my own function psrt() to sort strings in PL/pgSQL Language. Is there such SORTING function available for SQLite ?
Perhaps consider the following recursive CTE based example:-
This outputs:-
note rowid is used in the absence of a primary key to tie each individual letter/character to it's row. Obviously will not work with
WITHOUT ROWIDtables but could be adapted to then utilise the primary key.note LIMIT 50 was used to ensure that the loop ends (just in case), it will limit the above and should either be removed or increased to allow all data to be processed.
note that the above is case dependant so
ZzXxYyWwUuAaBbCcwill be re-ordered to beABCUWXYZabcuwxyz, so adjustments to the ORDER may be required.