Created
February 18, 2023 02:15
-
-
Save fdelbos/e7ce8914a6ad8aa36f37832c76ec0995 to your computer and use it in GitHub Desktop.
A shell script to calculate the days between 2 dates with postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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