Solved

SQL sorting

Posted on 2011-03-03
4
347 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
[X]
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
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

728 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