Skip to main content
Question

Evaluate true/false on string saved expression

  • May 22, 2019
  • 1 reply
  • 9 views

dimitrys

Hi,
if a have a stored expressions in a column, can I evaluate it to true or false?

for example,
I have a column with values like:
exp (column name)
1=1
1=1 and 'a'<>'b'

and want to get back:
exp eval_result
1=1 true
1=1 and 'a'<>'b' true
1=1 and 'a'='l' true

is it possible?

1 reply

Jim_Knicely
Forum|alt.badge.img+2
  • Participating Frequently
  • May 22, 2019

My immediate thought is to generate SQL statements to evaluate the expressions...

Example:

dbadmin=> SELECT * FROM boolean_operators;
       exp
------------------
 1=1
 1=1 and 'a'<>'b'
 1=1 and 'a'='l'
(3 rows)

dbadmin=> \! vsql -Atc "SELECT 'SELECT ''' || REPLACE(exp, CHR(39), CHR(39) || CHR(39)) || ''' exp, (SELECT ' || exp || ') eval_resuls;' FROM boolean_operators;" | vsql
 exp | eval_resuls
-----+-------------
 1=1 | t
(1 row)

       exp        | eval_resuls
------------------+-------------
 1=1 and 'a'<>'b' | t
(1 row)

       exp       | eval_resuls
-----------------+-------------
 1=1 and 'a'='l' | f
(1 row)

Or this:

dbadmin=> \! vsql -Atc "SELECT 'SELECT ''' || REPLACE(exp, CHR(39), CHR(39) || CHR(39)) || ''' exp, (SELECT ' || exp || ') eval_resuls ' || DECODE(LEAD(exp) OVER (ORDER BY 1), NULL, '', 'UNION ALL') FROM boolean_operators;" | vsql
       exp        | eval_resuls
------------------+-------------
 1=1 and 'a'='l'  | f
 1=1              | t
 1=1 and 'a'<>'b' | t
(3 rows)