3

I'm trying to escape a '%' character inside a format() function in PostgreSQL. The function replaces columns values based on a list of columns, deleting returns (\n) and trimming the strings.

CREATE OR REPLACE FUNCTION eliminar_retornos(text) RETURNS VOID
AS $$
 declare
 i text;
 fields text[] := ARRAY['direccion', 'localidad', 'calle', 'esq1', 'esq2', 'obs'];
 BEGIN
 FOREACH i IN ARRAY fields
 loop
 EXECUTE format(
 'update %1$s set %2$s = trim(upper(replace(%2$s, E''\n'', '' '')))', 1,ドル i)
 ;
 END LOOP;
 RAISE NOTICE 'Se actualizó la capa %', 1ドル;
 END
 ;
$$ LANGUAGE plpgsql;

I want to change this function to only replace those strings that have returns, using something like;

'update %1$s set %2$s = trim(upper(replace(%2$s, E''\n'', '' ''))) where %2$s LIKE E''%\n%''', 1,ドル i) 

The problem is that I don't know how to correctly escape the '%' for the 'LIKE' inside the format() function.

Sorry for posting something not reproducible, my knowledge is limited to achieve that.

asked Sep 8, 2021 at 15:03

1 Answer 1

7

The manual clearly says:

the special sequence %% may be used to output a literal % character

answered Sep 8, 2021 at 15:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.