Re: Conversion of string to int using digits at beginning

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Conversion of string to int using digits at beginning
Date: 2008-11-18 17:55:44
Message-ID: 20081118175544.GS2459@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 18, 2008 at 07:33:47PM +0200, Andrus wrote:
> I need to obtain max integer considering only numbers from start of column
> up to first non-integer character.
>
> I tried
>
> create temp table test (test char(20));
> insert into test values ('12');
> insert into test values ('23/3');
> insert into test values ('AX/3');
> select max(test::int) from test;

Maybe something like:

SELECT MAX(nullif(regexp_replace(test, '^([0-9]*).*$', E'\\1'),'')::INT);

The following parts of the manual may help:

http://www.postgresql.org/docs/current/static/functions-string.html
http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP

Sam

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Erwin Moller 2008-11-18 17:57:59 Re: Foreign Key 'walker'?
Previous Message Raymond O'Donnell 2008-11-18 17:51:08 Re: Conversion of string to int using digits at beginning