Skip to content

Instantly share code, notes, and snippets.

@fdelbos
Created February 18, 2023 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdelbos/e7ce8914a6ad8aa36f37832c76ec0995 to your computer and use it in GitHub Desktop.
Save fdelbos/e7ce8914a6ad8aa36f37832c76ec0995 to your computer and use it in GitHub Desktop.
A shell script to calculate the days between 2 dates with postgres
#!/bin/sh
echo "Use the following format: yyyy-mm-dd (ex: 2022-01-15)"
echo "First day: "
read FIRST_DAY
echo "Last day: "
read LAST_DAY
psql <<EOF
with sorted as (
select
least ('${FIRST_DAY}'::date, '${LAST_DAY}'::date) as first_day,
greatest ('${FIRST_DAY}'::date, '${LAST_DAY}'::date) as last_day
)
select
generate_series(sorted.first_day, last_day, '1 day'::interval)::date as days
from sorted;
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment