Re: text+number, find largest entry

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: gabor <gabor(at)nekomancer(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: text+number, find largest entry
Date: 2006-09-12 00:15:20
Message-ID: 8533.1158020120@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

gabor <gabor(at)nekomancer(dot)net> writes:
> i have a table, where there is a varchar(500) column,
> which contains data that is strangely formatted:
> it starts with letters, and ends with a number.
> for example:

> xyz001
> xyz002
> xyz044
> xyz1243
> abc01
> abc993
> abc2342

> now, for a given text-prefix (for example "xyz"), i need to
> find the record with the largest "numeric component".

I'd try using a couple of regexp_replace() calls to pull out the text
prefix and number separately. Then you could group by the one and order
by the other in your favorite variant of the DISTINCT ON pattern.
(See weather-report example in the SELECT reference page if you have
no idea what I'm talking about.)

> this lookup does not have to be especially fast.

Good ;-) ... otherwise changing your schema would definitely be indicated.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Robert Edwards 2006-09-12 00:43:44 Re: on connect/on disconnect
Previous Message gabor 2006-09-11 23:34:38 text+number, find largest entry