pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedText manipulation in SQL
3+ messages / 2 participants
[nested] [flat]
* Text manipulation in SQL
@ 2001-06-26 21:59 Jim <jim_esti@hotmail.com>
0 siblings, 2 replies; 3+ messages in thread
From: jim_esti@hotmail.com @ 2001-06-26 21:59 UTC (permalink / raw)
To: pgsql-sql
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?
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Text manipulation in SQL
@ 2001-06-27 15:24 Jim <jim_esti@hotmail.com>
parent: Jim <jim_esti@hotmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: jim_esti@hotmail.com @ 2001-06-27 15:24 UTC (permalink / raw)
To: pgsql-sql
Presto, Figured it out.
Select
(reverse(substr(reverse(trim(NAME)), (INSTR(reverse(trim(_NAME)), '
', 1, 1)), (length(reverse(trim(_NAME))))))) as LASTNAME,
(substr(trim(NAME), (length(trim(NAME))-(INSTR(reverse(trim(NAME)), '
', 1, 1)))+2, (length(trim(NAME))))) as FIRSTNAME
From
blabla_table;
It may look nasty but it does do exacty what was required.
jim_esti@hotmail.com (Jim) wrote in message news:<f0e3dc0b.0106261359.7ab6b281@posting.google.com>...
> 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?
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Text manipulation in SQL
@ 2001-07-06 04:23 Josh Berkus <josh@agliodbs.com>
parent: Jim <jim_esti@hotmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Josh Berkus @ 2001-07-06 04:23 UTC (permalink / raw)
To: jim_esti@hotmail.com; 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@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-07-06 04:23 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-06-26 21:59 Text manipulation in SQL Jim <jim_esti@hotmail.com>
2001-06-27 15:24 ` Jim <jim_esti@hotmail.com>
2001-07-06 04:23 ` Josh Berkus <josh@agliodbs.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox