Ignore characters when sorting

Posted on 2005-03-10
Medium Priority
Last Modified: 2012-06-27
I am using a MySql database. For one application I would like to ignore certain characters like
", (, {, [ when running a query with SORT in it.

Question by:SWB-Consulting
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 13513867

It would be something like

SELECT * FROM Table WHERE (col_a NOT LIKE '%(%') AND (col_a NOT LIKE '%{%') AND (col_a NOT LIKE '%[%') ORDER BY col_b


Expert Comment

ID: 13514012
vickerleung: I believe the OP still wants entries containing the characters included in the result set (and consequently the sort), he just doesn't want to sort based on those characters.

I suspect that there is no way to do this entirely in SQL, all though it shouldn't be too difficult to do programatically (in, say, PHP or Java).  You could even have your program create its own sorting table that contains the primary keys of each row in the original table and the value you want to sort by, stripped of any unwanted characters.  Then you could get a list sorted like you want without having to run your outside program every time.  The stripped value wouldn't have to appear in the result set, either:

select <cols> from orig_table,sort_table where orig_table.primary_key_field=sort_table.primary_key_field order by sort_table.sort_field;

Obviously you'd want to replace the table and attribute names with their correct values, and you can put whatever columns you want in place of <cols>.


Expert Comment

ID: 13514108
There is a possibility if you can find some function that can remove those characters you do not want to sort, REPLACE() is able to remove 1 char, but not so many different chars.

SELECT ....  ORDER BY REPLACE(col,'[','');
This will remove the character [ from col when mysql perform the order by or sorting.

I could not find a build-in string function to remove more than 1 character, maybe you want to consider writing your own User-Defined-Function. This is not so simple.
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

LVL 22

Accepted Solution

JesterToo earned 2000 total points
ID: 13514254
Nest multiple REPLACE's like this.. (example removes the characters  [  ]  $)

  SELECT ... ORDER BY REPLACE(REPLACE(REPLACE(Col,'[',''),']',''),'$','')  yada yada yada...

Author Comment

ID: 13520003
That's a great idea. Thanks!
LVL 22

Expert Comment

ID: 13521555
You're welcome... and thanks for the grade!

-- Lynn

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question