Solved

ORDER BY SYNTAX WITH CHARINDEX

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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ā€¦
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundrā€¦

770 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