Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Ora newbi question (whenever someone finds time)

Re: Ora newbi question (whenever someone finds time)

From: Galen Boyer <galenboyer_at_hotpop.com>
Date: 9 Jul 2002 21:20:15 -0500
Message-ID: <u65zo2lxy.fsf@hotpop.com>


On 9 Jul 2002, t.alexander_at_ads-tec.de wrote:
> Hello! An answer to your question
> " !!!!I'm new to the Oracle field !!!!
>
> Hi,
> Is it possible to perform simple Arithmetic Expressions in SQL?
> I imagine something like:
>

>> MagicBallCalculateThis '1 + 2 * (3 + 4)'; 
>>----------------------------------------------
>> 15 

>
> Reason:
> An application I am using is sending a Formula in form of a string of
> characters. In order to evaluate the Formula I would have to parse the
> formula to validate it,

Although directly running this in sqlplus produces the correct result,

    SQL>select 1 + 2 * (3 + 4) from dual;

     1+2*(3+4)


            15

I would still assume you would still need to parse it to validate it. If you can be assured that all strings are directly executable, then I guess this could be a way to go. But, remember that the database will still have to do the parsing step you are trying to avoid, and therefore you will be killing the shared sql area if this is high volume, because each one of these queries will be a new piece of sql.

I would bet you could do this in java. Hey, wait a minute, how bout the beanshell?

Go to www.beanshell.org. Download it, put the bsh.jar in your classpath. Open a shell buffer, type "java bsh.Interpreter" and then do the following.

bsh % String str = "1 + 2 * (3 + 4)";
bsh % print(eval(str));
15

This jar is now shipping with the latest weblogic, JEdit, Emacs's JDEE and others. You should be able to use it for this project as well.

> break it down into each arithmetic operater and convert the numeric
> strings to numbers.
>
> Wish:
> If I could send the complete string as an SQL string to Oracle and get
> the result returned, I would save myself a lot of work since these
> formulas only contain multiplication, addition and subtraction.

This would leave your database for dealing with data.

-- 
Galen deForest Boyer
Sweet dreams and flying machines in pieces on the ground.
Received on Tue Jul 09 2002 - 21:20:15 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US