Re: Text manipulation in SQL

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: jim_esti(at)hotmail(dot)com (Jim), pgsql-sql(at)postgresql(dot)org
Subject: Re: Text manipulation in SQL
Date: 2001-07-06 04:23:42
Message-ID: web-81558@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Jim,

> My little bit of code seems to only extract up to the first blank
> space. Which would only work correctly if the names where only in
> two
> parts (like Jane, A).
> As I said I would like to return the NAME column in two parts (the
> letters to the right of the right most blank, and the other half).
> It seems a little trick to me.
> Anyone have any advice, hints, or solutions?

You're on exactly the right track. Just extrapolate out the approach
you have already taken. For example, if the names were uniformly in the
form:

Last, Initials
James, A. G. P.

Then you can easily break off the parts as you have started, you just
need to fudge the cutoff points a little:

SUBSTR(Name,1,((INSTR(', ',1,1) - 1))

Which should give you:

Last (without comma or trailing space)
James

Then to grab the initials:

SUBSTR(Name,(INSTR(', ',1,1)+2))

Which will give you:

Initials (without comma or space).
A.G.P.

Of course, this would all be better placed in a PL/pgSQL function so
that you could test to see if the string contained ', ' before breaking
it up, look for NULL, etc. How about posting the functions when you
have them finished?

Finally, I hope that you are doing this string parsing for *conversion*
purposes. If you are designing a database to hold personal information,
*create seperate fields for the parts of the name!* This is much better
DB design, and will save you many headaches in the future.

Of course, I do realize that one doesn't always have design control when
dealing with legacy systems ...

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

Attachment Content-Type Size
unknown_filename text/plain 2 bytes
unknown_filename text/plain 2 bytes
unknown_filename text/plain 2 bytes

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bhuvan A 2001-07-06 04:42:10 Re: ConnecDB() -- couldn't send SSL negotiation packet:
Previous Message clayton cottingham 2001-07-05 22:23:51 Re: Re: automatic restore of database