Skip to content

Instantly share code, notes, and snippets.

@cetver
Last active August 29, 2015 14:04
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 cetver/20f54a59e0a024678428 to your computer and use it in GitHub Desktop.
Save cetver/20f54a59e0a024678428 to your computer and use it in GitHub Desktop.
Postgresql: split month by hours
with dates as (
select
(date_trunc('MONTH', NOW()))::timestamp without time zone --first date of month
+ concat(
generate_series(
0,
24
* to_char((date_trunc('MONTH', NOW()) + INTERVAL '1 MONTH - 1 day'), 'DD')::smallint --last day of month
-1
), --hours in month
' HOUR'
)::interval as date
)
select *
from dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment