Question

I have following query:

 select U.IdUser,U.memberID,SL.Title from users U
                   inner join statuslist SL on
                   U.idStatus=SL.IdStatus
                   where U.idOrg='1'and isnull(U.memberID,'')!=''
                   order by   U.memberID

It gives me result:

enter image description here

Here i want to have B12 MemberID to come before B110 (means 12 comes before 110 thats why).

How can i acheive it??

I tried below...

select U.IdUser,U.memberID,SL.Title from users U
                   inner join statuslist SL on
                   U.idStatus=SL.IdStatus
                   where U.idOrg='1'and isnull(U.memberID,'')!=''
                   order by     Substring(U.memberID,2,LEN(U.memberID))

But not giving correct results.

Please help me.

Was it helpful?

Solution

It is because of difference between order by with strings and order by with numbers:

select U.IdUser,U.memberID,SL.Title from users U
               inner join statuslist SL on
               U.idStatus=SL.IdStatus
               where U.idOrg='1'and isnull(U.memberID,'')!=''
               order by     CAST(Substring(U.memberID,2,LEN(U.memberID)-1) AS INT);

select U.IdUser,U.memberID,SL.Title from users U
               inner join statuslist SL on
               U.idStatus=SL.IdStatus
               where U.idOrg='1'and isnull(U.memberID,'')!=''
               order by     CONVERT(INT,Substring(U.memberID,2,LEN(U.memberID)-1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top