I have a table which has one TEXT column that looks like this:
0-4 mėn
5-12 mėn
1-3 metai
4-6 metai
7 metai ir daugiau
The text is in Lithuanian. Now the database was designed so it holds the values as TEXT and this is very bad, however I need to query it, to have the data sorted.
I have written this query:
SELECT DISTINCT `Age`,
SUBSTRING_INDEX(Age, " ", -1) as `AgePrefix`,
SUBSTRING_INDEX(Age, " ", 1) as `AgeValue`
FROM `suoPage`
ORDER BY `AgePrefix`, `AgeValue`
It does what I need, however the gotcha is “7 metai ir daugiau”. SUBSTRING_INDEX get’s AgePrefix for it as “daugiau” and so it appears as the first record. How can I solve this issue?
For sorting purpose you need to extract whole string after first space to do that you can use
SUBSTRINGfunction as: