From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Directly embedding a psql SET variable inside another string?
Date: Fri, 13 Jan 2023 10:12:30 -0600
Message-ID: <e556cfc8-30e6-014c-264a-f0b62d71a368@gmail.com> (raw)
Pg 12
I need to pass a \set variable (in this example named v_ssn) into a LIKE
string. A two-stage process (building v_like from v_ssn, and then using
v_like in the LIKE string) works, and is fine when executing an sql script,
but not so good is there any way to *directly* embed v_ssn in another string?
test=# \set v_ssn 345
test=# \echo :v_ssn
345
test=# \set v_like %:v_ssn%
test=# \echo :v_like
%345%
test=# SELECT * FROM employee WHERE ssn LIKE :'v_like';
ssn | name | ssn_int
-----------+----------+-----------
123456789 | John Doe | 123456789
(1 row)
As expected, this fails:
postgres=# SELECT * FROM employee WHERE ssn LIKE :'%v_ssn%';
ERROR: syntax error at or near ":"
LINE 1: SELECT * FROM employee WHERE ssn LIKE :'%v_ssn%';
--
Born in Arizona, moved to Babylonia.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: ronljohnsonjr@gmail.com
Subject: Re: Directly embedding a psql SET variable inside another string?
In-Reply-To: <e556cfc8-30e6-014c-264a-f0b62d71a368@gmail.com>
References: <e556cfc8-30e6-014c-264a-f0b62d71a368@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox