Karthik
Karthik

Reputation: 3301

How do I sort a varchar column using numeric ordering

I have the field receiptno, which is a varchar holding a numeric value, and want to sort this. Sorting as a string gives me the wrong ordering. I'd like to try sorting it as an integer. Is there anyway to converting to integer in the order by clause so I can sort by integer in the query itself.

Upvotes: 0

Views: 2598

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 158007

So, change the type of this column

Upvotes: 1

Adam Hopkinson
Adam Hopkinson

Reputation: 28793

You can use cast or convert to convert the field type:

... ORDER BY CAST(receiptno AS INTEGER) ASC

Edit sorry, fixed syntax

Upvotes: 6

Related Questions