Re: Extract only numbers from a varchar column

From: "Leandro Casadei" <mateamargo(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Extract only numbers from a varchar column
Date: 2008-05-05 19:09:56
Message-ID: 5983afbb0805051209i5f358552wca696ef508fbac83@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

---------- Forwarded message ----------
From: Leandro Casadei <mateamargo(at)gmail(dot)com>
Date: May 5, 2008 2:08 PM
Subject: Extract only numbers from a varchar column
To: pgsql-general(at)postgresql(dot)org

I need to extract only the first occurence of numbers from a column that is
varchar.

Here are a few examples of what I need to do:

abc200xx -> 200
340ppsdd -> 340
150ytyty555 -> 150

Can this be done by a simple query or do I need to use a cursor?

Thanks

I've finally solved this way:

select name, regexp_replace(name, '\\D*', '', 'g')::int as replaced
from sometable
where regexp_replace(name, '\\D*', '', 'g') != ''

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tino Wildenhain 2008-05-05 19:22:37 Re: operator is not unique: integer || integer
Previous Message Daniel Schuchardt 2008-05-05 19:04:41 Re: operator varchar = integer