Reputation: 245
For example I have 8am to 8pm.
I want the table where it can generate the table where it has entry one by one starttime
and endtime
(two columns):
8:00-9:00
9:00-10:00
....
19:00-20:00
Like this way. No date before the time.
Upvotes: 1
Views: 94
Reputation: 656391
Simpler, use a full timestamp with a dummy date component and cast to time
:
SELECT t::time AS starttime
, t::time + interval '1h' AS endtime
FROM generate_series('2000-1-1 08:00'::timestamp
, '2000-1-1 19:00'::timestamp
, interval '1h') t;
The same for text
columns:
SELECT to_char(t, 'HH24:MI') AS starttime
, to_char(t + interval '1h', 'HH24:MI') AS endtime
FROM generate_series('2000-1-1 08:00'::timestamp
, '2000-1-1 19:00'::timestamp
, interval '1h') t;
Upvotes: 0
Reputation: 154454
You can use the generate_series
function to do that:
psql# select generate_series('2014-01-01 16:00'::timestamp, '2014-01-01 20:00'::timestamp, '1 hour'); generate_series --------------------- 2014-01-01 16:00:00 2014-01-01 17:00:00 2014-01-01 18:00:00 2014-01-01 19:00:00 2014-01-01 20:00:00 (5 rows)
And then you can use:
SELECT
t AS starttime,
t + INTERVAL '1 hour' as endtime
FROM
GENERATE_SERIES(
'2014-01-01 16:00'::TIMESTAMP,
'2014-01-01 20:00'::TIMESTAMP,
'1 hour'
) AS t
To get the start and end times.
Alternatively, to just get the times, you can use:
SELECT
'08:00'::time + (t || ' hours')::interval as starttime,
'08:00'::time + ((t + 1)::text || ' hours')::interval as endtime
FROM
GENERATE_SERIES(0, 12) AS t
Upvotes: 1