What’s the best way to store the days of the week an event takes place on in a relational database?

I don’t think it’s hard to write queries if we use the bit option. Just use simple binary math. I think it’s the most efficient method. Personally, I do it all the time. Take a look:

 sun=1, mon=2, tue=4, wed=8, thu=16, fri=32, sat=64. 

Now, say the course is held on mon, wed and fri. the value to save in database would be 42 (2+8+32). Then you can select courses on wednesday like this:

select * from courses where (days & 8) > 0

if you want courses on thu and fri you would write:

select * from courses where (days & 48) > 0

this article is relevant: http://en.wikipedia.org/wiki/Bitwise_operation

you can put days of week numbers, as constants in your code and it will be clear enough.

Hope it helps.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)