From: Andrus <kobruleht2@hot.ee>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: How to create function returning numeric from string containing percent character
Date: Wed, 22 Jul 2020 13:49:53 +0300
Message-ID: <7B47085A6D564BA6BCEB1509909205BF@dell2> (raw)
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
I tried
CREATE OR REPLACE FUNCTION public.VAL(value text)
RETURNS numeric AS
$BODY$
SELECT coalesce(nullif('0'||substring(Translate($1,',','.'), '^-?[0-9]+\.?[0-9]*$'),''),'0')::numeric;
$BODY$ language sql immutable;
but if string contains % character,
select val('1,2%')
returns 0.
How to force it to return 1.2 ?
It should work starting from Postgres 9.0
Posted also in
https://stackoverflow.com/questions/63032072/how-to-create-function-returning-value-up-to-first-non-...
Andrus.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: kobruleht2@hot.ee
Subject: Re: How to create function returning numeric from string containing percent character
In-Reply-To: <7B47085A6D564BA6BCEB1509909205BF@dell2>
References: <7B47085A6D564BA6BCEB1509909205BF@dell2>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox