Ordering a string field with different lenghts

  • Dear all,

    I can't work out. I would need get ordered list from a string field but this own a dynamic contents. I'm looking for

    a method which provides me the ordered list by name field, how do I such thing?

    i.e:

    \\122.12.7.40\c\sistel\execv\dat\LOTNODOS.txt

    \\OFI0675P25\VARIOS\DETALLE.TXT

    c:\test\enr.txt

    Thanks in advance and regards,

    Enric

  • if you know the maximum length of the column

    select myfield

    from mytable

    order by convert(char(maxlen),myfield)

  • You asked:

    provide me the ordered list by name field, how do I such thing?

    i.e:

    \\122.12.7.40\c\sistel\execv\dat\LOTNODOS.txt

    \\OFI0675P25\VARIOS\DETALLE.TXT

    c:\test\enr.txt

    I am not sure what you mean.  What is the wanted result?  Is this (using the example)

    enr.txt

    DETALLE.txt

    LOTNODOS.txt

  • Yeah, it was just that.

     

  • A solution could be:

    Reverse the value of the field, find the backslash, copy it including the backslash, reverse it back and remove the backslash.

    When you are absolute sure that there is always a backslash you could also use:

    Reverse the value of the field, find the backslash, copy it excluding the backslash, reverse it back.

    Here is an example. Type this in the Query Analyzer to see how it wordks.

    declare @test-2 varchar(50) set @test-2 = '\\OFI0675P25\VARIOS\DETALLE.TXT'

    SELECT REPLACE(REVERSE(SUBSTRING(REVERSE(@Test),1,CHARINDEX('\',REVERSE(@Test)))),'\','')

  • that finally works properly:

      REVERSE(substring(REVERSE(DATASOURCE),1,charindex('\',REVERSE(DATASOURCE)) -1)) AS F

     

    Thanks to all for your comments.

  • OK, that is precise what I meant with my second solution.

    But it gives a problem when the string is empty, or contains no backslash.

    So, not knowing what values in real occur in your database, I gave you the most secure solution.

     

Viewing 7 posts - 1 through 6 (of 6 total)

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