Solved

ORDER BY SYNTAX WITH CHARINDEX

Posted on 2009-05-15
4
279 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
  • 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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now