Solved

Sorting of number in PTO order

Posted on 2009-05-13
4
381 Views
Last Modified: 2013-12-18
Hi ,

I have a table having  only one col. of varchar2 data type .
data are as follows :

1.2
2.0
2.21
8.0
2.2.1
3.0
4.1
3.1
5.1
3.2.4

I want to display is sorted order as :

1.2
2.0
2.2.1
2.21
3.0
3.1
3.2.4
4.1
5.1
8.0

How can I do it
in sql.

!Koushik
0
Comment
Question by:koushikonjob
  • 3
4 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 24381927
that's an interesting question because your sample data is sortable  "as is"

select your_column from your_table order by your_column

however, on the assumption that you want to sort your data from left-to-right in numeric order by each dot delimited number then try this...

This could be simplified by using a function for comparing values

I've made the assumption here that you will never have more than 9 numeric pieces to your string if that's not correct then adjust "level < 10" to some number big enough
I've also assumed no one number will be greater than 99 , if that's not correct then adjust "power(100,"  to some number bigger than the largest segment number
 SELECT   str

    FROM   (SELECT   str,

                     cnt,

                     maxcnt,

                     n,

                     TO_NUMBER(SUBSTR(str, startpos, endpos - startpos + 1)) * POWER(100, maxcnt - n)

                         val

              FROM   (SELECT   str,

                               cnt,

                               MAX(cnt) OVER () maxcnt,

                               n,

                               DECODE(n, 1, 1, INSTR(str, '.', 1, n - 1) + 1) startpos,

                               DECODE(n, cnt, LENGTH(str), INSTR(str, '.', 1, n) - 1) endpos

                        FROM   (SELECT   str, LENGTH(str) - LENGTH(REPLACE(str, '.', NULL)) + 1 cnt

                                  FROM   yourtable) t,

                               (    SELECT   LEVEL n

                                      FROM   DUAL

                                CONNECT BY   LEVEL < 10)

                       WHERE   n <= cnt))

GROUP BY   str

ORDER BY   SUM(val)

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24394407
need anything else?
0
 

Author Comment

by:koushikonjob
ID: 24395806
Sorry for late reply... ya its working fine.....

Thanks!!
Koushik
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 125 total points
ID: 24395851
glad I could help

I see this is your first question.  Welcome to  EE.

If you need nothing else on this topic then please close the question.
Accept the post with the answer and give a grade.
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
Queries 15 34
poor performance from  MySQL stored procedure 6 23
selective queries 7 22
Shredding xml into an oracle 11g Database 2 29
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

867 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

13 Experts available now in Live!

Get 1:1 Help Now