Solved

SQL sorting

Posted on 2011-03-03
4
345 Views
Last Modified: 2012-05-11
This should be easy but I'm losing my mind here. I need to sort a table. As it is the table gives the results like this

Code      Code description   CodeType
6532        NNNNNNN                    6
7853         MMMMMM                    7
653            VVVVVV                   6
785             LLLLLLLL                  7
0253        XXXXXX                        0
1200         Sme irrelevant           1
2250     BBBBBBBBBBB             2
225        XBXBX                           2
120        XXXXXXXXX                  1
025      ABCS                                0

What I need is this
0253    same desc as above          0
025       same -----------------           0
1200      same                                  1
1201      same ----------------            1
1205       same---------------
120       same -------------                 1
2250     same-----------------               2
225       same----------                         2
6532     same--------------                    6
653         same  -----------                    6
and so on. Basically what i need is Code column to be sorted by ASC order but each 3 length record should follow right after similarity ends. More precisely if code is 2250 and afterwards comes 2251, 2253,2254 this should end with 225. Code is nchar(10) field.
Hope I was clear
Thanks
0
Comment
Question by:fullstop
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35033368

select Code,      [Code description],   CodeType
from table1
order by case when (Len(Code) > 3) then Left(Code, 3) else Code end,    [Code description],   CodeType
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 125 total points
ID: 35033378
select Code
from @t
order by REPLACE(code,' ','z')

Open in new window

OR
select Code
from @t
order by RTRIM(code) + 'z'

Open in new window

0
 
LVL 22

Expert Comment

by:Thomasian
ID: 35033381
replace @t with your tablename
0
 

Author Closing Comment

by:fullstop
ID: 35033451
Thanks, works great
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

830 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