The Vertica Forum recently got a makeover! Let us know what you think by filling out this short, anonymous survey.
Please take this survey to help us learn more about how you use third party tools. Your input is greatly appreciated!

Evaluate true/false on string saved expression

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?

Answers

  • Jim_KnicelyJim_Knicely - Select Field - Administrator
    edited May 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)
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file