Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

564 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