Solved

# CARDINAL ORDERING SQL

Posted on 2011-03-11
363 Views
Hi guys

Is there any way that I can order a column in the order 1,2,3 instead of 1,11,12,2,3,30.  I am trying to write a script using SQL.
0
Question by:ernie_shah
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 35107936
Thanking you for any assistance.
0

LVL 12

Accepted Solution

enachemc earned 167 total points
ID: 35107943
order by to_number(column)
0

LVL 37

Assisted Solution

Geert Gruwez earned 167 total points
ID: 35108154

select lpad(column, 20, '0') sorter, ...
from table
order by sorter

0

LVL 32

Assisted Solution

Ephraim Wangoya earned 166 total points
ID: 35108831

That happens because your column is varchar.
You have to use a numeric field or convert the column to numeric when running you query

select *
from table
order by cast(Yourcolumn as int)

0

LVL 14

Expert Comment

ID: 35109976
why that? you can write like;
select thefield1youwant, thefield2youwant, thefield3youwant, thefield4youwant FROM table
much faster.

Oh,
Hello ewangoya,
Nice seeing you.
0

LVL 2

Expert Comment

ID: 35114456
Hello my Friend,
You can convert your field to numeric format in order part like this:
Select Field1, Field2 -- or some fields that you need
From ATable -- assume ATable is your table
Order by Cast(Field3 as int) -- assume Field3 is your ordering field
0

Author Comment

ID: 35202954
Thank you guys for the responses i will try the suggestions and let you know.......
0

LVL 37

Expert Comment

ID: 35421757
hey, my comment works too !

if you left pad a column with 0's it's sorted as a left padded string with 0's
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
###### Suggested Courses
Course of the Month2 days, 5 hours left to enroll