I need to do a query like this
select * from calendar where (select to_char(now(), 'day')) = true;
but this is invalid and fails with ERROR: failed to find conversion function from unknown to boolean.
The query I’m trying write, when run today would boil down to
select * from calendar where thursday = true;
But tomorrow, it should be
select * from calendar where friday = true;
The table has this schema
mbta=# \d calendar
Table "public.calendar"
Column | Type | Modifiers
------------+------------------------+-----------
service_id | character varying(255) | not null
monday | boolean |
tuesday | boolean |
wednesday | boolean |
thursday | boolean |
friday | boolean |
saturday | boolean |
sunday | boolean |
start_date | integer |
end_date | integer |
How can I write this query correctly?
That’s an ugly schema… A bunch of alternatives:
Replace ‘monday, tuesday… ‘ fields by a single integer field, to be interpreted as a bit mask – or use the bit-string data type
Replace them by a single field that contatins an array of integers (days of week).
Denormalize to an extra table, with a single day_of_week field and a FK to your calendar table.