Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ORDER BY SYNTAX WITH CHARINDEX

Posted on 2009-05-15
4
Medium Priority
?
291 Views
Last Modified: 2012-05-07
Hello Experts,
I need to sort a Select query by data enclosed in brackets at the end of a field. A sample of the data is below:
SPRAIN AND STRAIN OF CRUCIATE LIGAMENT OF KNEE [844.2]
DISLOCAT KNEE NOS-CLOSED [836.50]
FX TIBIA NOS-CLOSED [823.80]

Thanks in advance,

JackW9653
0
Comment
Question by:JackW9653
[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
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24398667
If the [] data will ALWAYS be at the end... you can use this.
select *,substring(a,charindex('[',a)+1,len(a)-charindex('[',a)-1)
from 
/* SAMPLE DATA STARTS HERE */
(
select 'SPRAIN AND STRAIN OF CRUCIATE LIGAMENT OF KNEE [844.2]' a
union select 'DISLOCAT KNEE NOS-CLOSED [836.50]'
union select 'FX TIBIA NOS-CLOSED [823.80]')b
/* SAMPLE DATA STOPS HERE */
order by substring(a,charindex('[',a)+1,len(a)-charindex('[',a)-1)

Open in new window

0
 

Author Comment

by:JackW9653
ID: 24398705
Brandon,
Thanks for the quick reply but I believe you misunderstood the "sample data". The data could be any number of over 16,000 codes.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 24398885
The only thing I may have misinterpreted is whether or not you want an alpha sort or numeric sort.  You can add as much data as you want.  Below is the updated which will do a numeric sort, and I have added more data to show you it will work regardless.

You have to replace "a" in the charindex statements with your column name.  And between the ./* COMMENTS */ put your table name.


select *,substring(a,charindex('[',a)+1,len(a)-charindex('[',a)-1)
from 
/* SAMPLE DATA STARTS HERE */
(
select 'SPRAIN AND STRAIN OF CRUCIATE LIGAMENT OF KNEE [844.2]' a
union select 'DISLOCAT KNEE NOS-CLOSED [836.50]'
union select 'FX TIBIA NOS-CLOSED [823.80]'
union select 'put whatever you want here [1.1]'
union select 'as long as you end with the right syntax [2.1'
)b
/* SAMPLE DATA STOPS HERE */
order by cast(substring(a,charindex('[',a)+1,len(a)-charindex('[',a)-1) as numeric(11,5))

Open in new window

0
 

Author Closing Comment

by:JackW9653
ID: 31582057
Thank you Brandon! It works great!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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