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

157
Vistas
Postgres March stange results

I'm tring to calculate time prorata between two dates over multi month periods using SQL on PostgresSQL.

My formula is pretty simple

select iot.*, 
       (iot.DATE_FIN - iot.DATE_DEBUT)::double precision / NULLIF(  extract (day from iot.DATE_FIN - date_trunc('month', iot.DATE_DEBUT)), 0::double precision) as prorata
from (
  select DATE '2020-05-01' as DATE_DEBUT,   DATE '2020-05-31' as DATE_FIN
) iot

Give me expected result => 1

But when I switch to march with

select iot.*,   
       (iot.DATE_FIN - iot.DATE_DEBUT)::double precision / NULLIF(extract (day from iot.DATE_FIN - date_trunc('month', iot.DATE_DEBUT)), 0::double precision) as prorata
from (
   select DATE '2020-03-01' as DATE_DEBUT, DATE '2020-03-31' as DATE_FIN
) iot

I've got a weird result of 1.0344827586206897

Any ideas?

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

0

This is caused by date_trunc('month', this function returns a date with a timezone, but your whole calculus doesn't handle the timezone.

If you force Postgres to ignore the timezone, it should work:

select iot.*,   
       (iot.DATE_FIN - iot.DATE_DEBUT)::double precision / NULLIF(extract (day from iot.DATE_FIN - date_trunc('month', iot.DATE_DEBUT)::timestamp without time zone), 0::double precision) as prorata
from (
   select DATE '2020-03-01' as DATE_DEBUT, DATE '2020-03-31' as DATE_FIN
) iot

I just cast date_trunc with ::timestamp without time zone.

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