Click here to Skip to main content
15,918,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Dear Friends,

I have an issue showing sorted record in grid view in 4.0. I have put order by clause in the query on date basis and after that on serial no basis like:-

"select * from [TableName] order by [Date] desc, [SerialNo] desc".

But my problem is that it sorts the data according to date but when it comes to sorting of record on the same date where serial no are like 37/1/1/ROR/20 and 37/1/1/ROR/9, It actually puts 37/1/1/ROR/9 before 37/1/1/ROR/20 as sql server 2008 pads a zero after a single digit hence making the serial no.

37/1/1/ROR/9 to 37/1/1/ROR/90 therefore putting it before 37/1/1/ROR/20.

And in the end the sorted list goes wrong.

Can anyone help me in this.?


Varun Sareen

1 solution

This will work
SELECT * FROM [TableName] ORDER BY [Date] DESC, CAST(REPLACE([SerialNo], '37/1/1/ROR/','') AS BIGINT) DESC

My suggestion is if you create additional columns by splitting the SerialNo column then it will be easy to handle these kind of things instead of above way.

SerialNo      - AC1 AC2 AC3 AC4 AC5
37/1/1/ROR/90 - 37  1   1   ROR 90

Note : AC - Additional column
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900