Re: last word in text

From: "Ian Harding" <ianh(at)tpchd(dot)org>
To: <gss+pg(at)cs(dot)brown(dot)edu>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: last word in text
Date: 2003-01-15 16:01:37
Message-ID: se2515d9.070@mail.tpchd.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

create function lastword(varchar) returns varchar as '
return [string range $1 [string last " " $1] end]
' language 'pltcl';

Ian Harding
Programmer/Analyst II
Tacoma-Pierce County Health Department
iharding(at)tpchd(dot)org
(253) 798-3549

>>> Gregory Seidman <gss+pg(at)cs(dot)brown(dot)edu> 01/15/03 07:16AM >>>
Is there a function or composition of functions that will retrieve the last
word in a text field? I have a table of addresses with arbitrary text
for the first field (i.e. to whom it is addressed), and I'd like to do my
queries as something like this:

SELECT * from Addresses
WHERE state = 'CA'
ORDER BY lastword(addressee);

What I'm missing is the definition/name of the lastword function.

Note that I'm not expecting perfect results from this. There will be some
weirdness with certain addressees. Nonetheless, it is a reasonable
approach. My other option, which is what I'm doing now, is to have a "last"
column in the table and have my host language fill it on insertion. It's
workable, but it's redundant and overly complex from a data abstraction
point of view.

--Greg

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
message can get through to the mailing list cleanly

Browse pgsql-general by date

  From Date Subject
Next Message Gregory Seidman 2003-01-15 16:13:11 Re: last word in text
Previous Message Gregory Seidman 2003-01-15 15:16:53 last word in text