Sorting Rows

  • 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

  • Try converting it to int and sort.

    Regards
    Durai Nagarajan

  • 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.

  • 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

  • @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.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply