Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yogonza524/63ca87cad5535007746b to your computer and use it in GitHub Desktop.
Save yogonza524/63ca87cad5535007746b to your computer and use it in GitHub Desktop.
SELECT current_date - (timestamp '2015-11-29')::date;
CREATE OR REPLACE FUNCTION betweenDays(fecha date)
RETURNS integer AS $$
DECLARE
output integer;
BEGIN
IF fecha IS NOT NULL THEN
SELECT current_date - fecha INTO output;
ELSE
RAISE EXCEPTION 'La fecha es nula';
END IF;
RETURN output;
END;
$$
LANGUAGE PLPGSQL
@yogonza524
Copy link
Author

Esta selección me permite obtener la diferencia en dias entre dos fechas. Los posibles resultados pueden ser positivos, cero o negativos.

@yogonza524
Copy link
Author

La función betweenDays(fecha) retorna el resultado de hacer la diferencia entre la fecha actual y la fecha pasada como parametro. Si la fecha parametro es nula entonces lanza una excepción.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment