views:

42

answers:

2

I want to sort one column fetched from mysql DB, and stored in an array. After fetching I am doing below steps.

  1. DB Fetching fields array in row format. ->Field1, Field2, Field3, Field4, Field5
  2. From that fields array One columns data [Field3], swapping string keywords. eg.
    AB013, DB131, RS001
    should become:
    013AB, 131DB, 001RS

Now I want to sort above value in new string format like-> 001RS, 013AB, 131DB

Its not like that, fix number for swapping string. To split and join string again there is a dynamic value which allow fetch data string to split that string from the given character position and then sort it again with new name.

A: 
select * from (
  select 
    Field1,
    Field2,
    CASE Field3
      WHEN 'AB013' THEN '013AB'
      WHEN 'DB131' THEN '131DB'
      WHEN 'RS001' THEN '001RS'
    END CASE AS NewField3,
    Field4,
    Field5 
  from Table1 
)
order by NewField3
tschaible
A: 

Why don't you let the DB do the work (for the fixed position data in your example):

select Field1
     , Field2
     , concat(  substr(Field3,3,3) 
               ,substr(Field3,1,2)
             ) as SwapedField3
     , Field4
     , Field5
from Table
order by SwapedField3

However, this doesn't take the 'dynamic value' for the split position into account...

  • Where does that value come from?
  • Can it be deduced from the data somehow?
lexu
Shail Patel
@Shail Patel: But couldn't you use that 'line code' to parameterize the "subste()" function, if you use the length() function? something like `substr(Field3,LineCode,length(Field3) - LineCode)`
lexu