From: Kumar <sgnerd@yahoo.com.sg>
To: Tomasz Myrta <jasiek@klaster.net>
Cc: psql <pgsql-sql@postgresql.org>
Subject: Re: How to avoid nulls while writing string for dynamic query
Date: Fri, 13 Feb 2004 14:44:24 +0530
Message-ID: <011101c3f211$c78d7c10$7502a8c0@hdsc.com> (raw)
References: <00c001c3f164$1db30140$7502a8c0@hdsc.com>
<402B74F4.7070604@klaster.net>
<006701c3f1ed$6610dc30$7502a8c0@hdsc.com>
<402C6FB8.8000606@klaster.net>
<00c801c3f200$e117eff0$7502a8c0@hdsc.com>
<402C85B3.2020204@klaster.net>
Thanks Tomasz Myrta. It is wonderful. I am still amazed from where you guys
knowing the options like quote_literal, etc.
Kumar
----- Original Message -----
From: "Tomasz Myrta" <jasiek@klaster.net>
To: "Kumar" <sgnerd@yahoo.com.sg>
Cc: "psql" <pgsql-sql@postgresql.org>
Sent: Friday, February 13, 2004 1:37 PM
Subject: Re: [SQL] How to avoid nulls while writing string for dynamic query
> Dnia 2004-02-13 08:13, Użytkownik Kumar napisał:
> > oh, ok understood.
> > What will happen for a timestamp field. Let us say c1 is a timestamp
column.
> >
> > sqlstr := 'insert into test(c1, c2) values
> > ('||'\''||COALESCE(rec.c1,'NULL')||'\','
> >
> >>> ||'\''||rec.c2||'\')';
> >
> >
> > If this case the query will be
> > insert into test(c1,c2) values ('2004-02-13', 'Hai')
> >
> > If there is a null value encountered i will return an error for the
> > following query
> > insert into test(c1,c2) values ('NULL', 'Hai')
> > ERROR: Bad timestamp external representation 'NULL'
> It's because you can't use quotes with null. Valid query is:
> insert into test(c1,c2) values (NULL, 'Hai');
>
> Your dynamic query will then look like:
>
> sqlstr := 'insert into test(c1, c2) values (' ||COALESCE('\'' || rec.c1
> || '\'','NULL') ...
>
> or more elegant:
>
> sqlstr := 'insert into test(c1, c2) values ('
> ||COALESCE(quote_literal(rec.c1),'NULL') ...
>
> Regards,
> Tomasz Myrta
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sgnerd@yahoo.com.sg, jasiek@klaster.net
Subject: Re: How to avoid nulls while writing string for dynamic query
In-Reply-To: <011101c3f211$c78d7c10$7502a8c0@hdsc.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