?
Solved

SQL sorting

Posted on 2011-03-03
4
Medium Priority
?
351 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

719 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