Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDMe-0001XR-8R for pgsql-general@arkaria.postgresql.org; Fri, 25 May 2018 14:06:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fMDMc-00041H-RB for pgsql-general@arkaria.postgresql.org; Fri, 25 May 2018 14:06:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDMc-0003vb-9X for pgsql-general@lists.postgresql.org; Fri, 25 May 2018 14:06:38 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fMDMZ-0006Xg-70 for pgsql-general@lists.postgresql.org; Fri, 25 May 2018 14:06:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C957D22326; Fri, 25 May 2018 10:05:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 25 May 2018 10:05:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=SaLL0z8/wwmDhIXMadvHedj1YoLW+ jKL0dwiiLc4Z9w=; b=okFwzqKOwSZjYGhHnQ+JwV9YRbYcaq0kuqFiSnBLUc4cm Z/8PPgO/G1ApTa3wTznEGr3mwEfgn0eW5Lo//8avxkUzatD5L4BayfMzViwgk/mA iP2z/Fcl5ogOFKGlxqbkdB/DgiU/4iT2Bs5dANx2QfLtN72XlS/VZW/XM2WtY9bc xHNh049XRShQN3SzmyCc4Xz456YWHU2PrBgihdK93JMHU9Ig1PYBUH3KhaQmUwLZ 6UphchkrQarD1VNpwAYlPJk4T0aduHGDcHdF8AVDyRmCL3Cb0JEt4ES9jeWDX0Ij 8Hrm2cdiufU2yE7yhoMbfbd/L2StKNjiuiCzfeWSw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=SaLL0z 8/wwmDhIXMadvHedj1YoLW+jKL0dwiiLc4Z9w=; b=GFREzEPwKAFAqKHE7BivPv nuQbYNBPyeLCiUsmTrB+nodtkaXKbe1HdZvBiTV/d6D0TCmbn9ZTesegm3hnUWKG CW72haKokkVVRYEf2DArUuDSwn2fFyCsy42DlUDsC1BlZ5pbzwncV7A3MJenbBM3 CgpCa1m4wwKcRt5n2yRTOoj39rf2x3oTgurhwxmtOfT2CJz2qPuuSZLcZ2owR9DG wLDPeukKUTq1olCi4gmBLcBN3tr1/dbKIqXyPAyLyjr13jBa5UhIYKtBoL2ad8jE AeFNdzN7JJyJnqVoX/g8De1CcLSdtOI061Y2A4Skz6BY4RLaZbpKVHe4YTVV6VPw == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id 342C3E4434; Fri, 25 May 2018 10:05:33 -0400 (EDT) Subject: Re: case statement within insert To: tango ward , "pgsql-generallists.postgresql.org" References: From: Adrian Klaver Message-ID: <626850f0-b6f8-3fdd-eaf0-6fcbff006737@aklaver.com> Date: Fri, 25 May 2018 07:05:32 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/25/2018 06:52 AM, Adrian Klaver wrote: > On 05/25/2018 02:04 AM, tango ward wrote: >> >> >> I want to insert data from mysql into a table in postgresql. I want to >> check when the subjectcode contains PE or NSTP so I can assign True or >> False to another column in destination DB. >> >> >> # Source data: >> >>   # Source data: MySQL >>     curr_msql.execute(''' SELECT code, subjectname >>                            FROM test_subj ''') >> >> # Destination >> for row in curr_msql: >>              curr_psql.execute(''' INSERT INTO subs ( >>                                                      created, modified, >>                                                      subjcode, subjname, >>                                                      is_pe_or_nstp) >> >>                                    VALUES (current_timestamp, >> current_timestamp, >>                                            %s, %s, >>                                            CASE >>          WHEN code like '%%PE%%' or code like '%%NSTP%%' > > Shouldn't the above be?: > > subjcode like '%%PE%%' or subjcode like '%%NSTP%%' Turns out that won't work as you cannot refer to a column in the CASE: cur.execute("insert into cell_per(category, cell_per, season, plant_type, short_category) values('herb test', 1, 'annual', CASE WHEN category like '%%herb%%' THEN 'herb' ELSE 'flower' END, 'ht' )") ProgrammingError: column "category" does not exist LINE 1: ...gory) values('herb test', 1, 'annual', CASE WHEN category l... ^ HINT: There is a column named "category" in table "cell_per", but it cannot be referenced from this part of the query. This works: cur.execute("insert into cell_per(category, cell_per, season, plant_type, short_category) values(%s, 1, 'annual', CASE WHEN %s like '%%herb%%' THEN 'herb' ELSE 'flower' END, 'ht' )", ('herb test', 'herb test')) So change code to row['code']? > >>          THEN True >>          ELSE False >>      END) ''', (row['code'], row['subjectname'])) >> >> I am getting TypeError: not all arguments converted during string >> formatting. >> >> Any advice pls? >> >> >> >> >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com