Connecting Tech Pros Worldwide Forums | Help | Site Map

eval function?

-
Guest
 
Posts: n/a
#1: Nov 23 '05
i am using a regular expression in a select statement inside a function.
how do i dynamically insert a variable into the regexp?

eg. SELECT string ~ '\\d{X}'

where X is the dynamic variable.

I tried '\\d{' || X || '}'

obviously it didn't work.

is there an equivalent of javascript's eval() in postgresql?

-
Guest
 
Posts: n/a
#2: Nov 23 '05

re: eval function?


- wrote:[color=blue]
> i am using a regular expression in a select statement inside a function.
> how do i dynamically insert a variable into the regexp?
>
> eg. SELECT string ~ '\\d{X}'
>
> where X is the dynamic variable.
>
> I tried '\\d{' || X || '}'
>
> obviously it didn't work.
>
> is there an equivalent of javascript's eval() in postgresql?[/color]

somebody......... help me out please..

i want to put a variable into a WHERE clause e.g

WHERE
columnname ~ X

(X is a variable).

how should i do it?

columnnane ~ '' || X ||'' doesn't work.
Stu
Guest
 
Posts: n/a
#3: Nov 23 '05

re: eval function?


- wrote:
[color=blue]
> - wrote:[color=green]
>> i am using a regular expression in a select statement inside a function.
>> how do i dynamically insert a variable into the regexp?
>>
>> eg. SELECT string ~ '\\d{X}'
>>
>> where X is the dynamic variable.
>>
>> I tried '\\d{' || X || '}'
>>
>> obviously it didn't work.
>>
>> is there an equivalent of javascript's eval() in postgresql?[/color]
>
> somebody......... help me out please..
>
> i want to put a variable into a WHERE clause e.g
>
> WHERE
> columnname ~ X
>
> (X is a variable).
>
> how should i do it?
>
> columnnane ~ '' || X ||'' doesn't work.[/color]

I'm not sure what you are asking. If you are talking about PL/pgSQL, and
you don't want to do something with the result set, then you can just build
the query in a varchar and run it using EXECUTE. Something like this:

CREATE OR REPLACE FUNCTION some_function(text) RETURNS BOOLEAN AS $$
DECLARE
my_query VARCHAR(100);
my_text AS ALIAS FOR $1;
BEGIN
my_query := 'SELECT * FROM some_table WHERE some_column = '
|| quote_literal(my_text) || ';';
EXECUTE my_query;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql;


Stu



Closed Thread