Search code examples
oracle-databaseselectplsqldecodeoracle8i

How to use SELECT result record as DECODE parameters?


Is it possible to use SELECT result as DECODE parameters when this SELECT returns only one record with prepared string? For example:

SELECT replace(replace(serialized_data)..)..) as result FROM table

Returns following result in ONE ROW:

0,'label0',1,'label1',2,'label2'

But when I put this to DECODE it's being interpreted as ONE PARAMETER. Is there possiblity to convert this result "string" to "pure" sql code? ;)

Thanks for any help.


Solution

  • You could kind of replicate decode by use of instr and substr. An example below (which could probably be tidied up considerably but works):

    select DTXT
          ,Nvl(
             Substr(
               DTXT
              ,Instr(DTXT, SEARCHTXT || VALMTCH)
               + Length(SEARCHTXT || VALMTCH)
              ,  Instr(DTXT, VALSEP, Instr(DTXT, SEARCHTXT || VALMTCH))
               - Instr(DTXT, SEARCHTXT || VALMTCH)
               - Length(SEARCHTXT || VALMTCH))
            ,CASEOTHER)
             as TXTMATCH
      from (select '0=BLACK;1=GREEN;2=YELLOW;' as DTXT
                  ,'1' as SEARCHTXT
                  ,';' as VALSEP
                  ,'=' as VALMTCH
                  ,'OTHER' as CASEOTHER
              from Dual)
    

    You do need to have a semi-colon (VALSEP) at the end of the text though to ensure you can find the last value (though it's possible to work around that if I looked into it further).