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

Select string split from table and then order by it – SQL

for example my table contains something like:

id name price cost
1 FRUIT:BANANA 12$ 13$
2 BANANA 15$ 14$
3 CUCAMBER 5$ 2$
4 PLASTIC:UMBRELLA 20$ 5$
5 PLASTIC:TABLE 10$ 50$
6 TABLE 12$ 20$

as you can see, some of my "names" contains ":" in the middle, when I make select query I want the result will contain.

id name price cost
1 BANANA 12$ 13$
2 BANANA 15$ 14$
3 CUCAMBER 5$ 2$
4 UMBRELLA 20$ 5$
5 TABLE 10$ 50$
6 TABLE 12$ 20$

I found some function that called STRING_SPLIT, but it works only if I from is after "FROM", not found function that I can use in SELECT.

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

Thanks

>Solution :

Use the function SUBSTRING_INDEX() to extract the part of the name after the : (if it exists):

SELECT id, 
       SUBSTRING_INDEX(name, ':', -1) name,
       price,
       cost
FROM tablename
ORDER BY name;

See the demo.

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