views:

42

answers:

2

I have a group of checkboxes where the user selects some of the weekdays (the opening days of a store). How can I save the selected days? Should I save something like 0111111 (zero means closed on sunday) on the same field and split the result when reading the data? Or create a field for each day and store 0 or 1 on each (weird)?

A: 

It totally depends on what you are going to do with that data once you have it. By and large, if these seven factoids (weekdays that the store is opn) are one fact, will be treated as one fact, and you'll never need to parse out and individually process one of the "factoids" within the string, then it can and possibly should be stored as one piece of data.

But, if you'll have to consider this infomation as seven distinct pieces of data, you'll be better off recording it as seven different pieces of data (i.e. seven different columns).

Again, there's a whole lot of "it depends" behind this. Another way to think of it is, look ahead to how you will be using the data, and try and structure your storage to simplify your processes (i.e. do you want to always be parsing string "1010101" for the element(s) you want?)

Philip Kelley
+2  A: 

The first approach saves space, the second improves performance while retrieving data, especially if you need to filter by individual weekday. It also makes the queries that manipulate weekdays a way clearer. You have to decide what is more important to you based on the application's logic.

In general, I'd use field-per-day solution.

a1ex07
I'd favor the second solution as well - with the first, you'll never be able to use an index to retrieve stores that are open any particular day except Sunday (the first character), so it will require a table scan. If it's a small table, no big deal, but still not very efficient. Since the days in a week won't change, I'd go with one field/day.
rwmnau
"retrieve stores that are open any particular day except Sunday". Good point. Ill go for the field/day option. Thanks to all.
JoaoPedro