Re: simple UPDATE statement...

From: Justin <justin(at)emproshunts(dot)com>
To: joshua(at)joshuaneil(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: simple UPDATE statement...
Date: 2008-03-11 21:29:51
Message-ID: 47D6F9CF.9060806@emproshunts.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

you need to strip the string apart using either regex which is
difficult to use or split_part()

http://www.postgresql.org/docs/8.3/interactive/functions-string.html

The update will look something like this...

Update customer set custfirstname = split_part(Name, ' ', 1) ,
custmiddlename = split_part(Name, ' ', 2) , custlastname =
split_part(Name, ' ', 3)

split_apart can give funky results if the data is not uniform.

Joshua wrote:
> Hello,
>
> I have a quick questions... consider the following information:
>
> I have a table 'customers' which looks like the following:
>
> firstname | middlename
> -------------------|--------------------------
> Johnathan C Mark S
> Joshua
> Susan T
> Jennifer
> Marcus D
> Mike G
> Ted
> William R
>
> I would like to write a SQL update statement that yields the following
> results:
>
> firstname | middlename
> -------------------|--------------------------
> Johnathan C Mark S
> Joshua Susan T
> Jennifer
> Marcus D
> Mike G
> Ted
> William R
>
>
> Basically, this... I need to take the middle initial from the
> 'firstname' field and place it in the 'middlename' field - that is if
> only there is a middle initial in the 'firstname' field.
>
> Please let me know the best way to write the UPDATE statement. If you
> have any questions or need more info from me please feel free to email.
>
> Thanks!!!
>
>
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2008-03-11 21:33:19 Re: How to convert postgres timestamp to date: yyyy-mm-dd
Previous Message Tom Lane 2008-03-11 21:28:25 Re: Trigger to run @ connection time?