Обсуждение: Text manipulation in SQL

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

Text manipulation in SQL

От
jim_esti@hotmail.com (Jim)
Дата:
Hi All,

I have a column with a variety of names in it, for example
John, Smith
John, A S
Jane, Doe A
Jane, A

I will call this column NAMES for this.
I was looking to manipulate that column.
Specifically:
The NAME field will be split after the rightmost blank.  Letter on the
right of that blank will be displayed as LAST_NAME, the remainder will
be displayed as FIRST_NAME.

Does anyone know how to do this correctly?

I have tried something like this:
LTRIM( NAME, (substr(NAME,1,(INSTR(NAME,' ',1,1)))))
But that would only yield something like this:
John,
John,
Jane,
Jane,

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?


Re: Text manipulation in SQL

От
"Josh Berkus"
Дата:
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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco

Вложения