Aeykash
Aeykash

Reputation: 135

Asc order in number column

Here is the query below and I want point column to be in ASC order, but when I do following query it don't ASC accordingly. Please see the result below

SELECT * 
FROM  `bonus` ORDER BY  `bonus`.`points` ASC 

It returns the following data:

id  referrer_id    points       created             modified
1   10             100      2013-06-01 00:00:00     2013-06-18 00:00:00
2   30             15       2013-06-01 00:00:00     2013-06-18 00:00:00
3   20             45       2013-06-01 00:00:00     2013-06-18 00:00:00

But I want points column to ASC like this

id  referrer_id     points      created             modified
1           10          100     2013-06-01 00:00:00     2013-06-18 00:00:00
2           20          45      2013-06-01 00:00:00     2013-06-18 00:00:00
3           30          15      2013-06-01 00:00:00     2013-06-18 00:00:00

Upvotes: 4

Views: 125

Answers (2)

bgs
bgs

Reputation: 3213

I think points column is varchar datatype, so you need manually casting into integer , you will got answer

SELECT * FROM  `bonus` ORDER BY  cast(bonus.points As int) asc

Create Table #Temp1(Id varchar(10))

insert into #Temp1 values(1)
insert into #Temp1 values(10)
insert into #Temp1 values(2)

Select Top 10 * from #Temp1 Order by Id 

1
10
2

Select Top 10 * from #Temp1 Order by cast(Id As int)

1
2
10
Drop Table #Temp1

Upvotes: 2

Saharsh Shah
Saharsh Shah

Reputation: 29051

Try this:

SELECT * FROM  bonus ORDER BY CAST(points AS INT) ASC;

Upvotes: 1

Related Questions