?
Solved

ORDER BY SYNTAX WITH CHARINDEX

Posted on 2009-05-15
4
Medium Priority
?
289 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

741 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