Search code examples
regexpostgresqlconditional-statementscaseregexp-replace

Extract numbers from a field in PostgreSQL


I have a table with a column po_number of type varchar in Postgres 8.4. It stores alphanumeric values with some special characters. I want to ignore the characters [/alpha/?/$/encoding/.] and check if the column contains a number or not. If its a number then it needs to typecast as number or else pass null, as my output field po_number_new is a number field.

Below is the example:

example

SQL Fiddle.

I tired this statement:

select 
(case when  regexp_replace(po_number,'[^\w],.-+\?/','') then po_number::numeric
else null
end) as po_number_new from test

But I got an error for explicit cast:

error


Solution

  • Simply:

    SELECT NULLIF(regexp_replace(po_number, '\D','','g'), '')::numeric AS result
    FROM   tbl;
    

    \D being the class shorthand for "not a digit".
    And you need the 4th parameter 'g' (for "globally") to replace all occurrences.
    Details in the manual.

    For a known, limited set of characters to replace, plain string manipulation functions like replace() or translate() are substantially cheaper. Regular expressions are just more versatile, and we want to eliminate everything but digits in this case. Related:

    But why Postgres 8.4? Consider upgrading to a modern version.

    Consider pitfalls for outdated versions: