Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sorting Rows Expand / Collapse
Author
Message
Posted Thursday, April 25, 2013 1:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, November 8, 2014 4:17 AM
Points: 206, Visits: 509
Dear,
I have a product table containing columns ProdID, ProdName and ...

The data type of ProdID is varchar(10). It contains data like 1,2,3 and so on.

If I execute the select statement,

select prodid from Product where (ProdId between '1000' and '11500') order by ProdId; 

I get the result like,
1000
10001
10002
10003
10004
1001
1002
.
.
.
11001
11002
1101
1102
.
.
.
11501


But I want that it should be like,

1000
1001
1002
1101
1102
10001
10002
10003
10004
11001
11501

What can I do for it. Please help me.



Regards,
Akbar
Post #1446329
Posted Thursday, April 25, 2013 2:15 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 10:45 AM
Points: 1,058, Visits: 2,696
Try converting it to int and sort.

Regards
Durai Nagarajan
Post #1446341
Posted Thursday, April 25, 2013 11:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:09 AM
Points: 10,342, Visits: 13,352
This is a common problem when numeric data is stored as character and durai has posted one solution. Often in these types of applications the application will prepend 0's to the numeric value to get it sorting as desired as well. Also be aware that any index on ProdID will be sorted the way SQL Server is returning the data as well.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1446660
Posted Friday, April 26, 2013 12:59 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 10:45 AM
Points: 1,058, Visits: 2,696
In addition to jack's comments.

why dont you user in or bigint and you can use identity as well.

Index will have greater effect on usage of between.



Regards
Durai Nagarajan
Post #1446818
Posted Saturday, April 27, 2013 10:01 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, November 8, 2014 4:17 AM
Points: 206, Visits: 509
@Durai, you are right and your idea worked.

Thank you @Durai and Jack.

Actually the application was developed six years ago and I am working only eight months on it. So it will take time for me to change even its difficult.
Post #1447306
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse