[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Sort Column in SQL Server

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
GRChandrashekar
Asked:
GRChandrashekar
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
QlemoC++ DeveloperCommented:
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
 
GRChandrashekarAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
awking00Commented:
So, if alpha includes A, B, and ABC, the order should be?
A
ABC
B
0
 
PortletPaulCommented:
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
 
awking00Commented:
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
 
QlemoC++ DeveloperCommented:
In which way is PortletPaul's code to prefer over my much more simple one?
0
 
PortletPaulCommented:
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
 
QlemoC++ DeveloperCommented:
Suppose equal split between http:#a39177320 and http:#a39177334 would be appropriate.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now