Обсуждение: Add one column to another

Поиск
Список
Период
Сортировка

Add one column to another

От
gvim
Дата:
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

От
Tony Capobianco
Дата:
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

От
"Oliveiros d'Azevedo Cristina"
Дата:
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

От
"David Johnston"
Дата:
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

От
Tim Landscheidt
Дата:
(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

От
Scott Marlowe
Дата:
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

От
vipul shah
Дата:
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