I have the below SQL that takes only data from the Name column up to the point where a space or hyphen appears. I would like to order the results by the output of the case statement but can’t find a way to do it.
select
case
when strpos("Name",' ') > 0 then substr("Name", 0, strpos("Name", ' '))
when strpos("Name",'-') > 0 then substr("Name", 0, strpos("Name", '-'))
else "Name"
end as StrippedName
from myTable
order by "StrippedName"
>Solution :
You could use a subquery:
select *
from (
select case ... end as col1
from ...
) as SubQueryAlias
order by
col1