Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
How to create function returning value up to first non-digit/decimal charcater

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
val('')  shound return 0

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

Update

Gordon Fiddle for 9.5 support returns null for not numeric string. I solved it using wrapper:

CREATE OR REPLACE FUNCTION public.VALwithNull(value text)
  RETURNS numeric AS
$BODY$
SELECT    replace( (regexp_matches($1, '^-?[0-9]+,*[0-9]*'))[1], ',', '.') ::numeric;
$BODY$ language sql immutable;

CREATE OR REPLACE FUNCTION public.VAL(value text)
  RETURNS numeric AS
$BODY$
SELECT coalesce( VALwithNull($1) ,0) ;
$BODY$ language sql immutable;

select  val('') , val('test'), val('-77,7%')

Is this best way to support 9.0+ ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think this regular expression does what you want:

replace( (regexp_match($1, '^-?[0-9]+,*[0-9]*') )[1], ',', '.')

Here is a db<>fiddle.

You can also use regexp_matches(). It does the same thing in this context.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda