Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id F2E5F9FB3E7 for ; Tue, 17 Jul 2007 23:42:53 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 66366-02 for ; Tue, 17 Jul 2007 23:42:50 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from web88311.mail.re4.yahoo.com (web88311.mail.re4.yahoo.com [216.39.53.234]) by postgresql.org (Postfix) with SMTP id CE8B49FB38F for ; Tue, 17 Jul 2007 23:42:50 -0300 (ADT) Received: (qmail 39208 invoked by uid 60001); 18 Jul 2007 02:42:49 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=rogers.com; h=X-YMail-OSG:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=2rMPncrePyU14xMngbGCHOXlti3GLK0S920DSlJay6dkcyMqVCaSqpjWinJfLZFBRJTByUtHUAvVsB6ClgenxYS/oJFsKOqnzCRq3t9a3CnE1350UkVLnwlC8+ZYCrQpalpSJTADPOXVAa3Zwi0ogIAcLozfHo+oN6WVAkn6bPQ=; X-YMail-OSG: jf.tdXsVM1k2h9buwXUyifxUiHgFKOTDmL0eNp7CpkfEh8yW12LMXmGVeVG1Y3Eu1Q-- Received: from [64.230.108.111] by web88311.mail.re4.yahoo.com via HTTP; Tue, 17 Jul 2007 22:42:48 EDT Date: Tue, 17 Jul 2007 22:42:48 -0400 (EDT) From: Norm Garand Subject: Inserting an IF statement in the middle of a SELECT in pl/pgSQL code To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <52149.39195.qm@web88311.mail.re4.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200707/106 X-Sequence-Number: 28841 Hi: I can't seem to resolve this issue. I have a fair sized SELECT statement that runs properly in a stored procedure using pl/pgSQL, but I have certain parts of the query that can filter by a NULL value, or a character string. What I can't seem to do is insert a conditional IF statement in my code. Here is how it starts: DECLARE r zrec_dd_holder%rowtype; BEGIN FOR r IN SELECT DISTINCT(pi.serial_number) AS "Incident ID", to_char(pi.incident_date,'Mon-dd-yyyy') AS "Incident date", to_char(pi.date_created,'Mon-dd-yyyy') AS "Report Date", CASE WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 1 THEN 1 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 2 THEN 1 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 3 THEN 1 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 4 THEN 2 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 5 THEN 2 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 6 THEN 2 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 7 THEN 3 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 8 THEN 3 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 9 THEN 3 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 10 THEN 4 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 11 THEN 4 WHEN EXTRACT ( MONTH FROM pi.incident_date ) = 12 THEN 4 END AS "Quarter", pf.name AS "Facility", pl.name AS "General Location", f.long_desc AS "Specific Location", ...... The user can choose to allow the default (in this case NULL) or supply a predetermined value for "Specific Location". In the WHERE portion of the SELECT statment, I'd like to insert the IF statement shown below. I've tried single and double quotes and the pipeline for concatenation, but nothing seems to work. Any suggestions or resolutions would be greatly appreciated. Please feel free to contact me directly. AND pi.id = pid.id AND ( pid.incident_type_cid BETWEEN 117 AND 123 ) /--------------- IF $7 IS NOT NULL THEN AND f.id = pid.specific_location_cid AND f.long_desc = $7 END IF ---------------/ AND ( pi.location_id = pl.id ) AND pf.id = pl.facility_id AND pi.person_status_code_id = b.id regards, Norm