evaluating equation stored in a string

From: "SunWuKung" <Balazs(dot)Klein(at)t-online(dot)hu>
To: pgsql-general(at)postgresql(dot)org
Subject: evaluating equation stored in a string
Date: 2006-04-26 14:57:59
Message-ID: 1146063479.818951.144740@v46g2000cwv.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have equations stored in strings and I would need to evaluate them in
pgsql - get the result.
Maybe there is an eval() function but I couldn't find it.

The only way I found was to use something like this:

calcstring_arg:= 'SELECT ' || calcstring_arg || ' ::numeric AS
outparam';
FOR tmp IN EXECUTE calcstring_arg LOOP END LOOP;
RETURN tmp.outparam;

However I have a problem with this cast. I don't know how to handle
brackets in the equation, since

SELECT 35/124 ::numeric
--0.28....

while

SELECT (35/124) ::numeric
-- 0

so I should do something like

SELECT (35::numeric/124::numeric)
--0.28....

But than I would need to start to look inside the string and do
manipulation to it that I would like to avoid. How could I evaluate
these strings independently from whether there are brackets in them or
not?

Thanks for the help.
Balázs

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ludwig Isaac Lim 2006-04-26 15:19:10 Re: Ordering of records in group by not possible
Previous Message Tony Caduto 2006-04-26 14:44:37 Question about postgresql.conf memory settings