?
Solved

Ignore characters when sorting

Posted on 2005-03-10
6
Medium Priority
?
2,100 Views
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.

Thanks,
Nima
0
Comment
Question by:SWB-Consulting
[X]
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
6 Comments
 
LVL 16

Expert Comment

by:vickerleung
ID: 13513867
SWB-Consulting

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

Vicker
0
 
LVL 2

Expert Comment

by:jnielsendotnet
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>.

JN
0
 
LVL 8

Expert Comment

by:lokus
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.

Example:
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.
0
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

by:
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...
0
 

Author Comment

by:SWB-Consulting
ID: 13520003
That's a great idea. Thanks!
0
 
LVL 22

Expert Comment

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

-- Lynn
0

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