1

I have a field that is populated with a series of criteria in a specific format:

CRITERIA
$1 = True
$2 > $3

The criteria information is stored in a table

CRIT_ID CRIT_DESCRIPTION
1       Example 1
2       Example 2
3       Example 3

The final output should look like this

CRITERIA
Example 1 = True
Example 2 > Example 3

Can anyone suggest how best to accomplish this? I have tried REPLACE and dabbled with REGEXP_REPLACE so far...

2 Answers 2

2

Perhaps not the most efficient, but it works recursively (that is, if the crit_description itself contains "placeholders", those are expanded as well. (A first solution, simpler than what's shown below, didn't do this recursive step.) See the third sample input which I added. I will post again later if I can clean it up some more.

NOTE: This assumes all "placeholders" are actually found in the criteria_info table; I didn't test what happens if they are not found. OP to state the requirement.

with
     inputs ( criteria ) as (
       select '$1 = True' from dual union all
       select '$2 > $3'   from dual union all
       select '$1 = $4'   from dual
     ),
     criteria_info ( crit_id, crit_description ) as (
       select 1, 'Example 1' from dual union all
       select 2, 'Example 2' from dual union all
       select 3, 'Example 3' from dual union all
       select 4, '$2 + $3'   from dual
     ),
     rec ( criteria, new_str ) as (
       select  criteria, criteria
         from  inputs        
       union all       
       select  r.criteria, 
               regexp_replace(r.new_str, '\$\d+', c.crit_description, 1, 1)
         from  rec r inner join criteria_info c
               on to_number(regexp_substr(r.new_str, '\$(\d+)', 1, 1, null, 1)) = c.crit_id
         where regexp_substr(r.new_str, '\$\d+') is not null
     )
select criteria, new_str
from   rec
where  regexp_substr(new_str, '\$\d+') is null
;


CRITERIA  NEW_STR
--------- ------------------------------------
$1 = True Example 1 = True
$2 > $3   Example 2 > Example 3
$1 = $4   Example 1 = Example 2 + Example 3

3 rows selected.
Sign up to request clarification or add additional context in comments.

Comments

0

In the following query I'm using the first SPACE as a separator between the id and the equation (and assuming that the id always start on the second char).

SELECT B.CRIT_DESCRIPTION || SUBSTR(A.CRITERIA, INSTR(A.CRITERIA, ' '))
  FROM A, B
 WHERE TO_NUMBER(SUBSTR(A.CRITERIA, 2, INSTR(A.CRITERIA, ' ') - 2)) = B.CRIT_ID

3 Comments

Thanks Neria, but this does not allow for multiple criteria appearing in the field (see line 2)
It also fails at runtime with an "invalid number" exception. Did you test the code before posting it?
@mathguy Thanks, I fixed the last parameter in the WHERE's SUBSTR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.