From: Adrian Klaver <adrian.klaver@aklaver.com>
To: tango ward <tangoward15@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Concatenate 2 Column Values For One Column
Date: Tue, 8 May 2018 21:21:22 -0700
Message-ID: <5e58b800-a9d6-dbb7-f366-ebe97bbc0506@aklaver.com> (raw)
In-Reply-To: <CAA6wQLJQFX6swLQfwcmqh+Pnp2=HUH11jAZg2XucTgYpi1U5Mg@mail.gmail.com>
References: <CAA6wQLJQFX6swLQfwcmqh+Pnp2=HUH11jAZg2XucTgYpi1U5Mg@mail.gmail.com>
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
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, tangoward15@gmail.com
Subject: Re: Concatenate 2 Column Values For One Column
In-Reply-To: <5e58b800-a9d6-dbb7-f366-ebe97bbc0506@aklaver.com>
References: <CAA6wQLJQFX6swLQfwcmqh+Pnp2=HUH11jAZg2XucTgYpi1U5Mg@mail.gmail.com> <5e58b800-a9d6-dbb7-f366-ebe97bbc0506@aklaver.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