Thread: Add one column to another

Add one column to another

From
gvim
Date:
I have to deal with a table which contains:

first_name
surname
email1
email2

... and I would like to create a view which combines both email columns thus:

first_name
surname
email

It looks simple but I can't think of an obvious query.

gvim


Re: Add one column to another

From
Tony Capobianco
Date:
Use the concat || operator.


On Thu, 2011-08-25 at 15:21 +0100, gvim wrote:
> I have to deal with a table which contains:
> 
> first_name
> surname
> email1
> email2
> 
> ... and I would like to create a view which combines both email columns thus:
> 
> first_name
> surname
> email
> 
> It looks simple but I can't think of an obvious query.
> 
> gvim
> 




Re: Add one column to another

From
"Oliveiros d'Azevedo Cristina"
Date:
Something like this...?

SELECT first_name,surname, email1 || ';' || email2
FROM t_your_table;


Best,
Oliver

----- Original Message ----- 
From: "gvim" <gvimrc@gmail.com>
To: "pgsql sql" <pgsql-sql@postgresql.org>
Sent: Thursday, August 25, 2011 3:21 PM
Subject: [SQL] Add one column to another


>I have to deal with a table which contains:
>
> first_name
> surname
> email1
> email2
>
> ... and I would like to create a view which combines both email columns 
> thus:
>
> first_name
> surname
> email
>
> It looks simple but I can't think of an obvious query.
>
> gvim
>
> -- 
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql 



Re: Add one column to another

From
"David Johnston"
Date:
I have to deal with a table which contains:

first_name
surname
email1
email2

... and I would like to create a view which combines both email columns
thus:

first_name
surname
email

It looks simple but I can't think of an obvious query.

-----------------------------------------------------------

SELECT first_name, surname, ARRAY[email1, email2] AS email
FROM [...]

David J.



Re: Add one column to another

From
Tim Landscheidt
Date:
(anonymous) wrote:

> I have to deal with a table which contains:

> first_name
> surname
> email1
> email2

> ... and I would like to create a view which combines both email columns thus:

> first_name
> surname
> email

> It looks simple but I can't think of an obvious query.

Try:

| SELECT first_name, surname, email1 AS email FROM testtable WHERE email1 IS NOT NULL UNION ALL
| SELECT first_name, surname, email2 AS email FROM testtable WHERE email2 IS NOT NULL;

Tim



Re: Add one column to another

From
Scott Marlowe
Date:
On Thu, Aug 25, 2011 at 8:52 AM, Oliveiros d'Azevedo Cristina
<oliveiros.cristina@marktest.pt> wrote:
> Something like this...?
>
> SELECT first_name,surname, email1 || ';' || email2
> FROM t_your_table;

If there's any nulls in email1 or email2 they'll need special handling
with coalesce.


Re: Add one column to another

From
vipul shah
Date:
Check this link.

It's useful to resolve the issue.

http://blog.ropardo.ro/2010/05/04/extending-postgresql-a-better-concat-operator/

- Vipul


On Thu, Aug 25, 2011 at 12:54 PM, Scott Marlowe <scott.marlowe@gmail.com> wrote:
On Thu, Aug 25, 2011 at 8:52 AM, Oliveiros d'Azevedo Cristina
<oliveiros.cristina@marktest.pt> wrote:
> Something like this...?
>
> SELECT first_name,surname, email1 || ';' || email2
> FROM t_your_table;

If there's any nulls in email1 or email2 they'll need special handling
with coalesce.

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql