I am trying to generate an available time slot function. for each day I am capturing start time and end time.
for eg: start time is 08:00 AM and end time is 03:00 PM.
I have another table that captures break time for the same day,
for eg: 11:00 AM to 11:15 AM and 01:00 PM to 1:30 PM.
And in another table I am storing already booked slots
for eg:
09:00 AM to 09:30 AM
11:15 AM to 11:45 AM
01:30 PM 02:00PM
I am using generate_series to generate time intervals between start and end time.
select * FROM generate_series(
timestamp '2016-11-09 08:00 AM',
timestamp '2016-11-09 03:00 PM',
INTERVAL '30m'
) t
I need to exclude the break time and already booked time from this generated series. I tried to use except function but this slot 11:15 AM to 11:45 AM is not able to remove. How do I generally write a condition to remove this?
Except query
SELECT t
FROM generate_series(
timestamp '2016-11-09 08:00 AM',
timestamp '2016-11-09 03:00 PM',
INTERVAL '1 hour'
) t
EXCEPT
SELECT concat(start_date,' ' , start_time)::timestamp as booked
FROM my_table where start_date::date = '2016-11-09';
my_table details
CREATE TABLE public.my_table
(
id bigint NOT NULL,
start_date character varying(500),
start_time character varying(500),
CONSTRAINT pk_my_table PRIMARY KEY (id))
WITH (
OIDS=FALSE
);