sorting a column

  • I have a column which has values as

    1
    2
    2.1
    3
    4
    2.1.1
    2.1.1.1
    2.1.2
    2.1.3
    2.2
    2.1.4
    2.3
    2.99
    2.99.1
    2.1.5
    2.99.1.1
    2.1.6
    2.100
    2.100.1
    2.101
    2.102
    2.1.7
     
    and i need to get this sorted as
     
    1
    2
    2.1
    2.1.1
    2.1.1.1
    2.1.2
    2.1.3
    2.1.4
    2.1.5
    2.1.6
    2.1.7
    2.2
    2.99
    2.99.1
    2.99.1.1
    2.100
    2.100.1
    2.101
    2.102
     
    Is this comething doable thru sql. any help will be greatly appreciated. Thanks
  • Have you tried ORDER BY ColumnX?  If YES and that failed you may need to apply an ORDER BY type INTEGER column and then use that for ordering purposes only.....



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • But how can you have this as an int column when you ahve data as 2.1.1.2 and so on...any further hint please??

  • I think this may work for you

    CREATE TABLE #Numbers( Number varchar(15))

    INSERT INTO #Numbers VALUES( '1')

    INSERT INTO #Numbers VALUES( '2')

    INSERT INTO #Numbers VALUES( '2.1')

    INSERT INTO #Numbers VALUES( '3')

    INSERT INTO #Numbers VALUES( '4')

    INSERT INTO #Numbers VALUES( '2.1.1')

    INSERT INTO #Numbers VALUES( '2.1.1.1')

    INSERT INTO #Numbers VALUES( '2.1.2')

    INSERT INTO #Numbers VALUES( '2.1.3')

    INSERT INTO #Numbers VALUES( '2.2')

    INSERT INTO #Numbers VALUES( '2.1.4')

    INSERT INTO #Numbers VALUES( '2.3')

    INSERT INTO #Numbers VALUES( '2.99')

    INSERT INTO #Numbers VALUES( '2.99.1')

    INSERT INTO #Numbers VALUES( '2.1.5')

    INSERT INTO #Numbers VALUES( '2.99.1.1')

    INSERT INTO #Numbers VALUES( '2.1.6')

    INSERT INTO #Numbers VALUES( '2.100')

    INSERT INTO #Numbers VALUES( '2.100.1')

    INSERT INTO #Numbers VALUES( '2.101')

    INSERT INTO #Numbers VALUES( '2.102')

    INSERT INTO #Numbers VALUES( '2.1.7')

    SELECT * FROM #Numbers

    ORDER BY CONVERT( decimal(10,8), SUBSTRING( REPLACE( Number, CHAR(46), ''), 1, 1) + '.' + SUBSTRING( REPLACE( Number, CHAR(46), ''), 2, 14) )

    DROP TABLE #Numbers

     

    I wasn't born stupid - I had to study.

  • if you have four dots('.') (IP addresses)

    then

    Select *

    from Table

    order by

      cast(parsename(ColX,4) as int)

     ,cast(parsename(ColX,3) as int)

     ,cast(parsename(ColX,2) as int)

     ,cast(parsename(ColX,1) as int)

     

     


    * Noel

  • darn but you stole my idea noel - have been saving parsename for just such an occasion as this...btw - sometime in the past (many moons ago) - i did test parsename with 2 and 3 dots & it worked equally fine with < 4.............







    **ASCII stupid question, get a stupid ANSI !!!**

  • ... worked equally fine with <= 4.............

     


    * Noel

  • Noel is correct. 

    Mine orders this like an Index to a document and that is not what you want.  (I did not notice that in your desired order). 

    I wasn't born stupid - I had to study.

  • have to have the last word don't you ?! okay - worked equally fine with <= 4!







    **ASCII stupid question, get a stupid ANSI !!!**

  • thanks but this  givesa  slightly different  result than expected.i need same as in my initial post. i guess i need to tweak the query to do that.

  • Noel, this result

    1        

    2        

    3        

    4        

    2.1      

    2.2      

    2.3      

    2.99     

    2.100    

    2.101    

    2.102    

    2.1.1    

    2.1.2    

    2.1.3    

    2.1.4    

    2.1.5    

    2.1.6    

    2.1.7    

    2.99.1   

    2.100.1  

    2.1.1.1  

    2.99.1.1 

     doesnt match up with my initial listing is there a way to get the result as posted earlier??thanks

  • Sorry try this!

    Select number 

    from MyTable

    order by

      ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),4) as int),0)

     ,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),3) as int),0)

     ,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),2) as int),0)

     ,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),1) as int),0)


    * Noel

  • Thanks you are awesome...

  • Happy to Help


    * Noel

  • Stealing ideas of everyone else, I suggest the following:

    SELECT * from #Numbers

    order by

    convert(int, reverse(parsename(reverse(Number),1))),

    convert(int, reverse(parsename(reverse(Number),2))),

    convert(int, reverse(parsename(reverse(Number),3))),

    convert(int, reverse(parsename(reverse(Number),4)))

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic. Login to reply