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 1fGGbe-0002hg-QS for pgsql-general@arkaria.postgresql.org; Wed, 09 May 2018 04:21:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fGGbd-0007Il-EX for pgsql-general@arkaria.postgresql.org; Wed, 09 May 2018 04:21:33 +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 1fGGbb-0007Ic-Jk for pgsql-general@lists.postgresql.org; Wed, 09 May 2018 04:21:33 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fGGbW-0006uj-JE for pgsql-general@lists.postgresql.org; Wed, 09 May 2018 04:21:31 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 0D26F22E37; Wed, 9 May 2018 00:21:24 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 09 May 2018 00:21:24 -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=fm2; bh=9suxUzceOqz2P8Qnbk92OM1RuKEaZ /n4tCnvHL1YVLg=; b=XXpIAf8f0eYl/GLsdhIyJSaZoBYvvFisQ3qc9xj7nwwQG N0LkC9KlxMSPZAXWjbxi6Ven18n7q+xqQt7tJEBHsGcjcQ3czq9sObWsKRiZaa64 4OuZREvapEm3VZ2jxbl1FEm3ddkhynxWGHVQ9iczrFmMW8WUgpHP+MAiDOnKN0wr 5avmriIICx8PPPxMzQSTUrvQS39vxdxTyhdbCliIhE2LN4tVP9azQkNMYzadZ0no ybbJKB7YmLyk9cGOA+3qWLMkreR2CI2YUnUCkwpocEyZ1mzlpzOr9Lw/UJEmcXsI ec/GT8ZRRawrjGR7CkzuA1yCQbx9sKjfqdYKa8Mcg== 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=9suxUz ceOqz2P8Qnbk92OM1RuKEaZ/n4tCnvHL1YVLg=; b=k6+B7Mc9E9izFYuoKATGjn PhA5mwEZr/op4bL0mv+zm0HZpJxa9fZDSnkFppDprWsWlE4v+oKsQMDgWZVsRAch 2yNwFdlHvY0V0qavs/+/mzXFJyF/0lAjucTQW8hYljNoqI1a1wCBZB6534n5jl1w Uba7U5lm8LO5GyEsmlwMXzXD0+f9LEXW+axrQdyhY/0TwXvVGITuetuld1qIenzO VWjkJcdMrSA73HQUB/xA3967JF4hnXV3h6Ep0V2//lkqpw9p8Pn3cmZIDj0qAhKN aqTq6Ak5UB5ZGkz9PQtueRoR/6DRfbV/3schcd9qLzaJ+MlvdelFyfAhJCumqinA == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id 647DDE4365; Wed, 9 May 2018 00:21:23 -0400 (EDT) Subject: Re: Concatenate 2 Column Values For One Column To: tango ward , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <5e58b800-a9d6-dbb7-f366-ebe97bbc0506@aklaver.com> Date: Tue, 8 May 2018 21:21:22 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.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/08/2018 07:17 PM, tango ward wrote: > Hi, > > Sorry for asking question again. > > I am trying to concatenate the value of column firstname and lastname > from source DB to name column of destination DB. > > My code so far: > > cur_t.execute(""" >                 SELECT firstname, lastname >                 FROM authors; >                 """) > > for row in cur_t: >     cur_p.execute(""" >                      INSERT INTO lib_author ( >                                                  created, modified, > last_name, >                                                  first_name, country, >                                                  school_id, name) >                     VALUES (current_timestamp, current_timestamp, %s, > %s, %s, >                             (SELECT id FROM ed_school WHERE name='My > Test School'), >                             (SELECT CONCAT(first_name, ',', last_name) > AS name FROM lib_author LIMIT 1) >                             ) >                     """, (row['lastname'], row['firstname'], '')) > > The code will take the first and lastname of the FIRST data existing on > the destination table. I modified the code, instead of running SELECT > and CONCAT, I passed string formatter and call the row['firstname'], > row['lastname'] > > for row in cur_t: >     cur_p.execute(""" >                      INSERT INTO lib_author ( >                                                  created, modified, > last_name, >                                                  first_name, country, >                                                  school_id, name) >                     VALUES (current_timestamp, current_timestamp, %s, > %s, %s, >                             (SELECT id FROM ed_school WHERE name='My > Test School'), >                              %s >                             ) >                     """, (row['lastname'], row['firstname'], '', > (row['firstname'], row['lastname']) ) > > The second code works but it includes the parenthesis in the DB. That is because: (row['firstname'], row['lastname']) is making a Python tuple for entry into the last %s. Not tested but try: (row['firstname'] + ', ' + row['lastname']) > > How can I remove the ( ) in the DB? I can't call the row['firstname'] > and row['lastname'] as values without using ( ). > > Any suggestion is highly appreciated. > > Thanks, > J -- Adrian Klaver adrian.klaver@aklaver.com