By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

order by for varchar datatype column with numeric values

P: 1
Hai every body,

i am using postgres 7.3 version, For a varchar (string) data column, in this column i filled the numbers (1,2,3 ...10,11,..... etc) alone. i sorted this column, but sorting values are comming 1,10,11,2,20.... etc like. but i need 1,2,3,10,22 like.
please tell me the solution for this problem.

thanks and regards,
Jul 20 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 534
If you always have integer values on this column it's better to define it as integer type, not a varchar.

As for your questions try one of the following:
Expand|Select|Wrap|Line Numbers
  1. select * from tab_name order by column_name::integer;
  2. select * from tab_name order by cast(column_name AS integer);
  3. select * from tab_name order by int8 (column_name);
You can replace * with column_name or whatever select list you may need
Jul 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.