0

I am using Sql server 2008 r2, I need to order by the following data:

CardNo

R-1  
R-2
R-12
R-1A
R-3
R-2B

Result should look like this

CardNo

R-1  
R-1A
R-2
R-2B
R-3
R-12

I have tried different combinations in order by clause but of no use like:

select * from [Coll2012-13] where  
    SUBSTRING(CardNo, 1, 1) IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z') 
    AND SUBSTRING(CardNo, 2, 1) IN ('-') 
    AND SUBSTRING(CardNo, 3, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
    and Landmark='Anandbagh' order by LEN(CardNo),CardNo ASC
0

5 Answers 5

1

Assumption: Values are always of the format 'Letter-Alphanumeric string'

Try this:

select card_no
from [Coll2012-13]
order by left(card_no,1),
case
when isnumeric(substring(card_no,3, len(card_no))) = 1
then cast(substring(card_no,3, len(card_no)) as int)
else cast(substring(card_no,3, patindex('%[A-Z]%',substring(card_no,3, len(card_no)))-1) as int)
end,
case 
when patindex('%[A-Z]%', substring(card_no,3,len(card_no))) > 0
then substring(card_no,patindex('%[A-Z]%', substring(card_no,3,len(card_no)))+2,1)
end

How this works: First check the starting letter. Next, check if the alphanumeric part is in fact only numeric. If so, get the integer value of that part. If it is not, get the numeric part of it and use that as the sort value. Finally, if the alphanumeric part does contain a letter, use that as another sort value.

Demo here.

Sign up to request clarification or add additional context in comments.

6 Comments

Invalid length parameter passed to the LEFT or SUBSTRING function.
Any chance you have null values? Did you try in the demo link I provided?
hi thanks its working i need to check with the table if any null values or any other things are there
@user1939665 No problems, should be simple to filter or handle nulls
hey thank u very much for the query it working. I really appreciate ur work thanks for the help
|
0

You should replace all non digit chars with "" and convert the rest in a number. Then sort by the number.

You can try to use a function as described here SQL Server 2000: how do I return only the number from a phone number column for this

4 Comments

SELECT CardNo, REPLACE(REPLACE(REPLACE(REPLACE(CardNo,' ',''),'(',''),')',''),'-','') AS NewPhone FROM [COLL2012-13] where Landmark='Anandbagh'
the above is working but the right result im getting it can u send me an query how to get this result
Sorry. I don't understand. Please update your question with detailed description
I have a table where cardno are inserted as R-1, R-2, R-3, R-4, R-2A, R-1A, R-12, R-10, R-14, R-12A...........The result i need it as Cardno:- R-1, R-1A, R-2, R-2A, R-3, R-4, R-10, R-12, R-12A, R-14
0

A simple - but 'should be maintained' - solution is to create a sorting table and join that table to your result set. The table contains all CardNo values and associates a sort order to them.

EDIT:

CREATE TABLE CardNoOrderHelper (
    CardNo VARCHAR(16)
    , OrderRank INT CONSTRAINT DF_CardNoOrderHelper_OrderRank DEFAULT 0
    , PRIMARY KEY CLUSTERED (
        CardNo ASC
    )
);

-- Fill your table with the expected sort order (dinstinct insert, than adjust the order ranks)

SELECT
    *
FROM
    [Coll2012-13] AS T
    LEFT JOIN CardNoOrderHelper CH
        ON T.CardNo = CH.CardNo
ORDER BY
    T.OrderRank

As I said, you have to maintain this table. When your resultset is small this can be done manually.

4 Comments

hi im new to this can u be little bit clear how to do that with an example im stuck with this
manually its not possible as there are nearly 3000 data in each areas
Then combine the calculated order rank and the order helper table.
Does this pattern always applied? I mean, are there any rekords where the CardNo is not in that form? Are there any additional rules to generate CardNo (for example the last two characters are hexadecimal numbers, or there could be R-1W or R-12M?)
0

Try this

with cte
AS
(
    select 
       * 
       ,substring(CardNo, 1, charindex('-')-1) RealRank1
       ,substring(CardNo, charindex('-')+ 1, 10) RealRank2
    from 
       [Coll2012-13] 
)
select 
    *
from 
    cte
order by
    RealRank1
    ,RealRank2

2 Comments

im getting error as charindex should have 2 0r 3 arguments
charindex(cardno, '-',1)
0

I have the similar problem. I have used this query below.

To use this query u must know the ID (string) maximum length and I have adjusted the format in this query to use Mark-Number+Alphabet (example R-1,R-1A, R-11, R-11A, R-1AA,R-1B)

Query:

select 
 b.CardNo, b.separatorIndex
 ,b.Mark, b.Mark_length
 ,case b.isNumericMark1 + b.isNumericMark2 + b.isNumericMark3
   when 1 then cast (b.Mark1 as int)
   when 2 then cast (b.Mark1 + b.Mark2 as int) 
   when 3 then cast (b.Mark1 + b.Mark2 + b.Mark3 as int) 

  end as Mark1
from
(
    select 
        a.CardNo
        ,charindex('-',a.cardNo,0) as separatorIndex
        , len(a.cardNo) - charindex('-',a.cardNo,0) as Mark_length
        , substring(a.CardNo,0,charindex('-',a.cardNo,0)) as Mark

        , substring(a.CardNo,charIndex('-',a.cardNo,0)+1,1) as Mark1
        , isnumeric(substring(a.CardNo,charIndex('-',a.cardNo,0)+1,1*1)) as isNumericMark1

        , substring(a.CardNo,charIndex('-',a.cardNo,0)+2,1) as Mark2
        , isnumeric(substring(a.CardNo,charIndex('-',a.cardNo,0)+2,1)) as isNumericMark2

        , substring(a.CardNo,charIndex('-',a.cardNo,0)+3,1) as Mark3
        , isnumeric(substring(a.CardNo,charIndex('-',a.cardNo,0)+3,1)) as isNumericMark3

    from [Coll2012-13] a
) b
order by Mark,Mark1,Mark_length

Result:

CardNo               separatorIndex Mark                 Mark_length Mark1
-------------------- -------------- -------------------- ----------- -----------
R-1                  2              R                    1           1
R-1A                 2              R                    2           1
R-2                  2              R                    1           2
R-2B                 2              R                    2           2
R-3                  2              R                    1           3
R-12                 2              R                    2           12

Hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.