Question

I have been asked to work with a legacy Oracle 9i DB and compare fractional values which users have stored as strings. ie. some table field may contain values like "9 7/8", "3 15/16" etc.

Without modifying the DB, adding procedures, indexes or anything how could I query for all the records who's value is less than "7 4/5"?

Like:

SELECT * FROM `ANNOYING_TABLE` WHERE FRACTIONAL_STRING_TO_DECIMAL(`fractional_data`) < 7.8

Is there some way to perhaps split on the " " and the split the second value on "/" and then do split[0]+split[1]/split[2]

SELECT * FROM `ANNOYING_TABLE`, FIRST_SPLIT = SPLIT(`fractional_data`," "), SECOND_SPLIT = SPLIT( FIRST_SPLIT[1], "/" ) WHERE (FIRST_SPLIT[0]+(SECOND_SPLIT[0] / SECOND_SPLIT[1])) < 7.8

Any ideas?

Was it helpful?

Solution 2

you are asking to execute a string or varchar2 as equation, the result is a value of this equation. right?

here is a solution for this, I assume the space in "7 4/5" means "7+ 4/5" or you have to modify the function bellow to meet your requirements.

create or replace function get_equation_val( p_equation varchar2) return number is
  eq        varchar2(500);
  eq_stmt   varchar2(500);
  eq_result number;
begin

  eq      := replace(p_equation, ' ', '+');
  eq_stmt := 'select ' || eq || ' from dual';
  execute immediate eq_stmt
    into eq_result;
 -- dbms_output.put_line(eq_result);
 return(eq_result);
end get_equation_val;

and here is a test for this function:

select get_equation_val('7 4/5') from dual;

OTHER TIPS

I got it by some contortions of built in functions...

SELECT
    "TABLE"."ID",
    "TABLE"."VALUE"
FROM
    "TABLE"
WHERE
(
    SUBSTR(
        VALUE,
        0,
        INSTR(VALUE, ' ')- 1
    ) + SUBSTR(
        VALUE,
        INSTR(VALUE, ' ')+ 1,
        INSTR(VALUE, '/')- INSTR(VALUE, ' ')- 1
    ) / SUBSTR(
        VALUE,
        INSTR(VALUE, '/')+ 1
    ) > 9.5
)

It's not elegant, but it runs and thats all I need. Hopes this helps some other unlucky person stuck with legacy Oracle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top