Solved

Sort Column in SQL Server

Posted on 2013-05-18
11
386 Views
Last Modified: 2013-05-28
Hi,

I have two columns in table MEMBERFAMILY called MEMBERNO and SPOUSENO.

The Data stored in these columns are as follows

MEMBERNO          SPOUSENO
A-1                         A-2
B-11                       B-101
B-14                       B-114

I need the ouput AS USERNO after sort as under in one column. While sort it is first sorted by alpha and then numeric.

Delimiter "-" is there in all the data

USERNO
A-1
A-2
B-11
B-14
B-101
B-114
0
Comment
Question by:GRChandrashekar
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39177292
If we can rely on a fixed format:
select * from
(select memberno as userno from memberfamily
union
select spouseno from memberfamily
) data
order by left(userno,1), substring(userno, 3, 10)

Open in new window

0
 

Author Comment

by:GRChandrashekar
ID: 39177301
Only delimiter is constant.
Alpha may be A, CRPB, XYZDDES and so on
Similarly length of number also is not fixed, it may be 0,10023342342, 34 and so on
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39177320
I was afraid of that. Besides, the secondary sort expression was incorrect.
select userno from
(select memberno as userno, charindex('-', memberno) as idx from memberfamily
union
select spouseno, charindex('-', spouseno)  from memberfamily
) data
order by left(userno,idx), cast(substring(userno, idx+1, 100) as bigint)

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39177332
So, if alpha includes A, B, and ABC, the order should be?
A
ABC
B
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39177334
select
  code
from (
      select
          MEMBERNO as code
        , substring(MEMBERNO,1,charindex('-',MEMBERNO)-1) as alpha
        , cast(substring(MEMBERNO,charindex('-',MEMBERNO)+1,50) as int) as num
      from MEMBERFAMILY
      union all
      select
          SPOUSENO
        , substring(SPOUSENO,1,charindex('-',MEMBERNO)-1)
        , cast(substring(SPOUSENO,charindex('-',MEMBERNO)+1,50) as int)
      from MEMBERFAMILY
      ) ms
order by alpha, num

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39177354
PortletPaul beat me to it (again) but thought I would still post my solution -
select * from
(select memberno as userno from memberfamily
 union all
 select spouseno as userno from memberfamily) as x
order by substring(userno,1,charindex('-',userno)-1),
cast(substring(userno,charindex('-',userno)+1,len(userno)) as int;
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39177363
In which way is PortletPaul's code to prefer over my much more simple one?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39187912
I haven't tested it but his proposed solution does indeed appear to meet the requirements.  
I  can't offer any opinion as to why mine was chosen.

I wish to add is that my suggestion was done without reference to Qlemo's
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39190531
Suppose equal split between http:#a39177320 and http:#a39177334 would be appropriate.
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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

24 Experts available now in Live!

Get 1:1 Help Now