Re: How to create function returning numeric from string containing percent character - Mailing list pgsql-general

From David G. Johnston
Subject Re: How to create function returning numeric from string containing percent character
Date
Msg-id CAKFQuwb+Mjc3QLxiP5dKD0qK9163uZXc1EtuECFZR=qR_WQH+Q@mail.gmail.com
Whole thread Raw
In response to How to create function returning numeric from string containing percent character  ("Andrus" <kobruleht2@hot.ee>)
List pgsql-general
On Wed, Jul 22, 2020 at 3:50 AM Andrus <kobruleht2@hot.ee> wrote:
val function should return numeric value from string up to first non-digit character, considering first decimal point also:

    val('1,2TEST')  should return 1.2
    val('1,2,3')  should return 1.2
    val('-1,2,3')  should return -1.2

    SELECT coalesce(nullif('0'||substring(Translate($1,',','.'), '^-?[0-9]+\.?[0-9]*$'),''),'0')::numeric;
select val('1,2%')
How to force it to return 1.2 ?

It should work starting from Postgres 9.0

Removing the $ from your regexp pattern should work for the 4 provided examples.

You cannot remove stuff from the end of a string if you require that the end of said string match what you want to return.

David J.

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: CASCADE/fkey order
Next
From: Surya Widyanto
Date:
Subject: Re: Slow or Cannot Connect to PostgreSQL Instance Service on Windows 10