Solved

SQL sorting

Posted on 2011-03-03
4
339 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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

18 Experts available now in Live!

Get 1:1 Help Now