pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDirectly embedding a psql SET variable inside another string?
6+ messages / 4 participants
[nested] [flat]
* Directly embedding a psql SET variable inside another string?
@ 2023-01-13 16:12 Ron <ronljohnsonjr@gmail.com>
0 siblings, 2 replies; 6+ messages in thread
From: Ron @ 2023-01-13 16:12 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Directly embedding a psql SET variable inside another string?
@ 2023-01-13 17:08 Adam Scott <adam.c.scott@gmail.com>
parent: Ron <ronljohnsonjr@gmail.com>
1 sibling, 2 replies; 6+ messages in thread
From: Adam Scott @ 2023-01-13 17:08 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Do you mean like this?
postgres=# \set v_embed %:v_ssn%
postgres=# \echo :v_embed
%345%
postgres=# SELECT * FROM employee WHERE ssn LIKE :'v_embed';
ssn | name
-----------+----------
123456789 | John Doe
(1 row)
On Fri, Jan 13, 2023 at 8:12 AM Ron <ronljohnsonjr@gmail.com> wrote:
>
> 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.
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Directly embedding a psql SET variable inside another string?
@ 2023-01-13 17:13 David G. Johnston <david.g.johnston@gmail.com>
parent: Ron <ronljohnsonjr@gmail.com>
1 sibling, 1 reply; 6+ messages in thread
From: David G. Johnston @ 2023-01-13 17:13 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Jan 13, 2023 at 9:12 AM Ron <ronljohnsonjr@gmail.com> wrote:
>
> is there any way to *directly* embed v_ssn in another string?
>
No
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%';
>
>
Two options:
format('%%%s%%', :'v_ssn')
'%' || :'v_ssn' || '%'
David J.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Directly embedding a psql SET variable inside another string?
@ 2023-01-13 17:13 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Adam Scott <adam.c.scott@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: Adrian Klaver @ 2023-01-13 17:13 UTC (permalink / raw)
To: Adam Scott <adam.c.scott@gmail.com>; Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On 1/13/23 09:08, Adam Scott wrote:
> Do you mean like this?
>
> postgres=# \set v_embed %:v_ssn%
> postgres=# \echo :v_embed
> %345%
> postgres=# SELECT * FROM employee WHERE ssn LIKE :'v_embed';
> ssn | name
> -----------+----------
> 123456789 | John Doe
> (1 row)
>
>
Actually that replicates what Ron got to work. What Ron is looking for
is to skip the intermediate step and do:
\set v_ssn 345
SELECT * FROM employee WHERE ssn LIKE :'%v_ssn%';
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Directly embedding a psql SET variable inside another string?
@ 2023-01-13 17:14 David G. Johnston <david.g.johnston@gmail.com>
parent: Adam Scott <adam.c.scott@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: David G. Johnston @ 2023-01-13 17:14 UTC (permalink / raw)
To: Adam Scott <adam.c.scott@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Please don't top-post.
On Fri, Jan 13, 2023 at 10:08 AM Adam Scott <adam.c.scott@gmail.com> wrote:
> Do you mean like this?
>
> postgres=# \set v_embed %:v_ssn%
> postgres=# \echo :v_embed
>
> test=# \set v_like %:v_ssn%
>> test=# \echo :v_like
>>
>>
Aside from the names those look identical...
David J.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Directly embedding a psql SET variable inside another string?
@ 2023-01-13 18:07 Ron <ronljohnsonjr@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Ron @ 2023-01-13 18:07 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On 1/13/23 11:13, David G. Johnston wrote:
> On Fri, Jan 13, 2023 at 9:12 AM Ron <ronljohnsonjr@gmail.com> wrote:
>
>
> is there any way to *directly* embed v_ssn in another string?
>
>
> No
> 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%';
>
>
> Two options:
>
> format('%%%s%%', :'v_ssn')
This one looks clean, and works perfectly:
> '%' || :'v_ssn' || '%'
--
Born in Arizona, moved to Babylonia.
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2023-01-13 18:07 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13 16:12 Directly embedding a psql SET variable inside another string? Ron <ronljohnsonjr@gmail.com>
2023-01-13 17:08 ` Adam Scott <adam.c.scott@gmail.com>
2023-01-13 17:13 ` Adrian Klaver <adrian.klaver@aklaver.com>
2023-01-13 17:14 ` David G. Johnston <david.g.johnston@gmail.com>
2023-01-13 17:13 ` David G. Johnston <david.g.johnston@gmail.com>
2023-01-13 18:07 ` Ron <ronljohnsonjr@gmail.com>
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