Re: another simple SQL question

From: "Nicholas Barr" <nicky(at)chuckie(dot)co(dot)uk>
To: "Joshua" <joshua(at)joshuaneil(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-novice(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: Re: another simple SQL question
Date: 2007-06-25 15:41:02
Message-ID: 23992.62.244.190.66.1182786062.squirrel@www.chuckie.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-sql

> Ok here is another simple question from a novice....
>
> Here is what my table looks like
>
> firstname lastname fullname
> ---------- ---------- -----------
> smith, john
> green, susan
> white, jeff
>
>
> How can I break the fullname field into firstname lastname fields so it
> looks like the following:
>
> firstname lastname fullname
> --------- --------- ---------
> john smith smith, john
> susan green green, susan
> jeff white white, jeff
>
> Please let me know. Sorry for such simple novice questions, I appreciate
> your support.
>
> THANKS!
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

temp=# create table temptable3 (firstname varchar(32), lastname
varchar(32), fullname varchar(32)) without oids;
CREATE TABLE
temp=# insert into temptable3 (fullname) values ('smith, john');
INSERT 0 1
temp=# insert into temptable3 (fullname) values ('spencer, frank');
INSERT 0 1
temp=# select * from temptable3; firstname | lastname | fullname
-----------+----------+----------------
| | smith, john
| | spencer, frank
(2 rows)

temp=# update temptable3 set firstname=trim(substring(fullname from
position(',' in fullname) + 1)), lastname=trim(substring(fullname from 0
for position(',' in fullname)));
UPDATE 2
temp=# select * from temptable3; firstname | lastname | fullname
-----------+----------+----------------
john | smith | smith, john
frank | spencer | spencer, frank
(2 rows)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nicholas Barr 2007-06-25 15:44:01 Re: Move a database from one server to other
Previous Message Adrián Ribao Martínez 2007-06-25 15:36:23 Move a database from one server to other

Browse pgsql-novice by date

  From Date Subject
Next Message Daniel T. Staal 2007-06-25 15:44:20 Re: another simple SQL question
Previous Message Joshua 2007-06-25 15:28:40 another simple SQL question

Browse pgsql-sql by date

  From Date Subject
Next Message Daniel T. Staal 2007-06-25 15:44:20 Re: another simple SQL question
Previous Message Ireneusz Pluta 2007-06-25 15:33:29 Re: simple SQL question