No dejes de divulgar nuestro espacio y códigos en tus redes sociales, ayúdanos a aumentar nuestra comunidad.
Ejemplo 1: segundos de diferencia de fecha de postgres
-- difference in seconds between two dates
select extract(epoch from ('2020-03-30 09:55:56'::timestamp -'2020-03-30 08:54:55'::timestamp));
-- result:3661
Ejemplo 2: postgresql obtiene la diferencia entre dos fechas
select age('2010-04-01','2012-03-05'),
date_part('year',age('2010-04-01','2012-03-05')),
date_part('month',age('2010-04-01','2012-03-05')),
date_part('day',age('2010-04-01','2012-03-05'));
Ejemplo 3: postgres obtiene el número de días entre dos fechas
SELECT
AGE('2012-03-05','2010-04-01'),
DATE_PART('year',AGE('2012-03-05','2010-04-01')) AS years,
DATE_PART('month',AGE('2012-03-05','2010-04-01')) AS months,
DATE_PART('day',AGE('2012-03-05','2010-04-01')) AS days;
Ejemplo 4: diferencia de tiempo de postgres en minutos
CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIME, end_t TIME)
RETURNS INT AS $$
DECLARE
diff_interval INTERVAL;
diff INT = 0;
BEGIN
-- Minus operator for TIME returns interval 'HH:MI:SS'
diff_interval = end_t - start_t;
diff = DATE_PART('hour', diff_interval);
IF units IN ('hh','hour') THEN
RETURN diff;
END IF;
diff = diff *60+ DATE_PART('minute', diff_interval);
IF units IN ('mi','n','minute') THEN
RETURN diff;
END IF;
diff = diff *60+ DATE_PART('second', diff_interval);
RETURN diff;
END;
$$ LANGUAGE plpgsql;
Te mostramos las comentarios y valoraciones de los lectores
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)