MySQL
Perhaps like this:
SELECT val,
val REGEXP '^[a-zA-Z]',
CAST(val AS DECIMAL(14,4))
FROM mytesting
ORDER BY CASE WHEN val REGEXP '^[a-zA-Z]' >= 1 THEN 1 ELSE 0 END ASC,
CAST(val AS DECIMAL(5,2)) ASC;
First, I use CASE
expression REGEXP
to check whether the leading value is alphabet or number; if the value starts with alphabet, I assign to 1
if not I'll assign it to 0
so it'll be on the top of an ascending order. Then I add a second order where I change the val
datatype to decimal using CAST
. I put both operation in SELECT
to see what the value it return after the filtering. Since the purpose they're in SELECT
is for viewing purpose only, you can remove them from the final query so like this should work:
SELECT val
FROM mytesting
ORDER BY CASE WHEN val REGEXP '^[a-zA-Z]' >= 1 THEN 1 ELSE 0 END ASC,
CAST(val AS DECIMAL(5,2)) ASC;
Alternatively, if you found those values to be useful and you want to use them, you can simplify the query to something like this:
SELECT val,
CASE WHEN val REGEXP '^[a-zA-Z]' >= 1 THEN 1 ELSE 0 END AS val_check,
CAST(val AS DECIMAL(14,4)) AS val_convert
FROM mytesting
ORDER BY val_check ASC,
val_convert ASC;
Demo fiddle
Well, as you may already guess, the answer above was for MySQL and posted before the tag change. Unfortunately, Oracle is not my daily database however in the spirit of not trying to post a wrong answer, I did this:
SELECT val,
CASE WHEN REGEXP_LIKE(val,'^[0-9]')
THEN CAST(REGEXP_REPLACE(val,'[a-zA-Z]','')+0 AS DEC(5,2))
WHEN REGEXP_REPLACE(val,'[^a-zA-Z]','') IS NULL
THEN CAST(val AS DEC(5,2))
ELSE 9000*9000 END AS val_check
FROM mytesting
ORDER BY CASE WHEN REGEXP_LIKE(val,'^[0-9]')
THEN CAST(REGEXP_REPLACE(val,'[a-zA-Z]','')+0 AS DEC(5,2))
WHEN REGEXP_REPLACE(val,'[^a-zA-Z]','') IS NULL
THEN CAST(val AS DEC(5,2))
ELSE 9000*9000 END,
val;
With the intention of trying to emulate the same idea as the MySQL suggested solution above.
Demo fiddle