2

I have postgres function in which i am appending values in query such that i have,

DECLARE
    clause text = '';

after appending i have some thing like,

clause = "and name='john' and age='24' and location ='New York';"

I append above in where clause of the query i already have. While executing query i am getting "and" just after "where" result in error

How to use regex_replace so that i remove the first "and" from clause before appending it to the query ?

2
  • Have you tried leave 'and' from the start of clause? I don't think manipulating an sql query with regular expressions is a good idea. Commented May 27, 2014 at 9:17
  • it will append and in query because there are conditions on which it is appending query and it is not possible to stop it thats why after appending i am removing it from clause Commented May 27, 2014 at 9:23

5 Answers 5

2

Instead of fixing clause after the fact, you could avoid the problem by using concat_ws (concatenate with separator):

clause = concat_ws(' and ', "name='john'", "age='24'", "location ='New York'") 

will make clause equal to

"name='john' and age='24' and location ='New York'"
Sign up to request clarification or add additional context in comments.

Comments

2

This can be even simpler. Use right() with a negative offset.

Truncates the first n characters and you don't need to specify the length of the string. Faster, simpler.

Double quotes (") are for identifiers in Postgres (and standard SQL) and incorrect in your example. Enclose string literals in single quotes (') and escape single quotes within - or use dollar quoting:
Insert text with single quotes in PostgreSQL

Since this is a plpgsql assignment, use the proper assignment operator :=. The SQL assignment operator = is tolerated, too, but can lead to ambiguity in corner cases.

Finally, you can assign a variable in plpgsql at declaration time. Assignments in plpgsql are still cheap but more expensive than in other programming languages.

DECLARE
   clause text := right($$and name='john' and age='24' ... $$, -5)

All that said, it seems like you are trying to work with dynamic SQL and starting off on the wrong foot here. If those values can change, rather supply them as values with the USING clause of EXECUTE and be wary of SQL injection. Read some of the related questions and answers on the matter:
https://stackoverflow.com/search?q=[plpgsql]+[dynamic-sql]+EXECUTE+USING

Comments

1

You do not need regex:

clause = substr(clause, 5, 10000);
clause = substr(clause, 5, length(clause)- 4); -- version for formalists

Comments

1

concat_ws sounds like the best option, but as a general solution for things like this (or any sort of list with a delimiter) you can use logic like (pseudocode):

delim = '';
while (more appendages)
    clause = delim + nextAppendage;
    delim = ' AND ';

Comments

0

If you want to do it with regular expression try this:

result = regexp_replace(clause, '^and ', '')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.