Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order by case statement that extracts substring from column

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use a subquery:

select  *
from    (
        select  case ... end as col1
        from    ...
        ) as SubQueryAlias
order by
        col1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading