pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to create function returning numeric from string containing percent character
3+ messages / 3 participants
[nested] [flat]

* How to create function returning numeric from string containing percent character
@ 2020-07-22 10:49  Andrus <kobruleht2@hot.ee>
  0 siblings, 2 replies; 3+ messages in thread

From: Andrus @ 2020-07-22 10:49 UTC (permalink / raw)
  To: pgsql-general

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.






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to create function returning numeric from string containing percent character
@ 2020-07-22 22:44  David G. Johnston <david.g.johnston@gmail.com>
  parent: Andrus <kobruleht2@hot.ee>
  1 sibling, 0 replies; 3+ messages in thread

From: David G. Johnston @ 2020-07-22 22:44 UTC (permalink / raw)
  To: Andrus <kobruleht2@hot.ee>; +Cc: 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.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to create function returning numeric from string containing percent character
@ 2020-07-29 15:16  Ben Madin <ben@ausvet.com.au>
  parent: Andrus <kobruleht2@hot.ee>
  1 sibling, 0 replies; 3+ messages in thread

From: Ben Madin @ 2020-07-29 15:16 UTC (permalink / raw)
  To: Andrus <kobruleht2@hot.ee>; +Cc: pgsql-general

I suspect it will depend on your localisation whether you need to account
for different decimal separators, but just in case:

SELECT replace(substring('-1,2%' from '^-?\d*[.,]?\d*'), ',', '.')::numeric;

On Wed, 22 Jul 2020 at 18:50, 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
>
> 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.
>
>
>
>

-- 

[image: Ausvet Logo] <https://www.ausvet.com.au/;

Dr Ben Madin

BVMS MVPHMgmt PhD MANZCVS GAICD
Managing Director
Mobile:
+61 448 887 220 <+61448887220>
E-mail:
ben@ausvet.com.au
Website:
www.ausvet.com.au
Skype: benmadin
Address:
5 Shuffrey Street
Fremantle, WA 6160
Australia

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-07-29 15:16 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-07-22 10:49 How to create function returning numeric from string containing percent character Andrus <kobruleht2@hot.ee>
2020-07-22 22:44 ` David G. Johnston <david.g.johnston@gmail.com>
2020-07-29 15:16 ` Ben Madin <ben@ausvet.com.au>

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