Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 order by asc

Author  Topic 

sqldev6363
Yak Posting Veteran

54 Posts

Posted - 2009-01-06 : 13:51:12
Hi,

I have table tbl1 having column col1 contains values

col1

10
100
20
30
50
60


When I am displaying in ascending order it is displaying in the above order

But i need to display in this order

col1

10
20
30
40
50
60
100

How can i display in the above order???

Can any one help me?


dev

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 13:52:48
What is the data type of col1? I believe your answer is going to be varchar or similar. In order to get that kind of sort, you can either modify col1 so that it is using the proper data type or you can convert/cast the data in the query.

ORDER BY CONVERT(int, col1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sqldev6363
Yak Posting Veteran

54 Posts

Posted - 2009-01-06 : 14:13:23
Yes, data type is varchar because, i have one more row which is 'tot'

col1

10
100
20
30
40
50
tot

These are the values i have??
Then how i will get the asc order??


quote:
Originally posted by tkizer

What is the data type of col1? I believe your answer is going to be varchar or similar. In order to get that kind of sort, you can either modify col1 so that it is using the proper data type or you can convert/cast the data in the query.

ORDER BY CONVERT(int, col1)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




dev
Go to Top of Page

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2009-01-06 : 15:04:14
Order By
IsNumeric(col1) Desc,
Case When IsNumeric(col1) = 1 Then Convert(Int, col1) End,
col1
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-01-06 : 15:10:01
I would consider converting the column to an int datatype and getting rid of the "tot" row. You should do your calculations separately rather than keep the total as one of the rows.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-06 : 23:23:56
select col1 from urtable order by cast(col1 as int)
Go to Top of Page
   

- Advertisement -