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
Solved

ORDER BY SYNTAX WITH CHARINDEX

Posted on 2009-05-15
4
283 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

There are some very powerful Data 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 discu…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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