Solved

CARDINAL ORDERING SQL

Posted on 2011-03-11
10
356 Views
Last Modified: 2012-05-11
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
Comment
Question by:ernie_shah
10 Comments
 

Author Comment

by:ernie_shah
ID: 35107936
Thanking you for any assistance.
0
 
LVL 12

Accepted Solution

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

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 167 total points
ID: 35108154
use lpad format the number as char with left padding zero's

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

0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:systan
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

by:RezaSadigh
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

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

Expert Comment

by:Geert Gruwez
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Press three keys together and trigger a function 3 49
index Out OF Range Exception error 4 53
Basic Java Case or If-Else statement... 3 43
DBGrid or StringGrid ? 6 62
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now