Last active
May 27, 2024 19:48
-
-
Save ismael3s/f2bb275c81c5a45fcc887f000d4b98c9 to your computer and use it in GitHub Desktop.
Calculate Working Hours between two dates in 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
create or replace | |
function calculate_working_hours(start_time TIMESTAMPTZ, | |
end_time TIMESTAMPTZ) | |
returns interval as $$ | |
declare | |
date record; | |
minutes interval := interval '0 minutes'; | |
begin | |
for date in | |
select | |
* | |
from | |
generate_series(start_time, | |
end_time, | |
'1 minutes') | |
where | |
generate_series::time between interval '09:00:00' and interval '18:00:00' | |
and extract('dow' | |
from | |
generate_series) not in (0, 6) | |
loop | |
minutes := minutes + interval '1 minutes'; | |
end loop; | |
return minutes; | |
end; | |
$$ language plpgsql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment