X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 0238CD1D26D for ; Fri, 13 Feb 2004 09:13:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 98237-09 for ; Fri, 13 Feb 2004 05:13:44 -0400 (AST) Received: from smtp015.mail.yahoo.com (smtp015.mail.yahoo.com [216.136.173.59]) by svr1.postgresql.org (Postfix) with SMTP id 2165AD1D141 for ; Fri, 13 Feb 2004 05:13:42 -0400 (AST) Received: from unknown (HELO IVESIA67) (sgnerd@203.196.190.138 with login) by smtp015.mail.yahoo.com with SMTP; 13 Feb 2004 09:13:42 -0000 Message-ID: <011101c3f211$c78d7c10$7502a8c0@hdsc.com> Reply-To: "Kumar" From: "Kumar" To: "Tomasz Myrta" Cc: "psql" 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> Subject: Re: How to avoid nulls while writing string for dynamic query Date: Fri, 13 Feb 2004 14:44:24 +0530 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-2" Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/141 X-Sequence-Number: 16832 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" To: "Kumar" Cc: "psql" 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