views:

29

answers:

3

Most of the sites capture the date (e.g. birth date) information as either a single entity (i.e via a calendar control) or instead as separate fields (date, month, year).

  1. Are there any benefits in showing them as separate fields?
  2. Are they represented in the database as DATE data type or as separate fields.

I am just trying to understand the pros & cons of representing the date field.

+2  A: 

Never break up a date into separate database fields. It's one thing: a date. It's in the database as a single date field.

You don't break up an amount into separate database fields for dollars and cents. How would you do math? You'd have to reconstruct the number from the pieces.

You don't break up other numbers into separate database fields for individual digits. How would you do math? You'd have to reconstruct the number from the pieces.

The same is true for dates. You rarely need the pieces. When you do, simple functions will provide the pieces. You more often need the entire date to do math with.

Further, the pieces of a date (specifically the month and day) have such complex relationships (28 to 31 days per month based on seemingly random rules) that you never want to attempt date arithmetic in your own application software. Use libraries. Use the database libraries.

The UI display may have separate fields to make it "easier" on the users. [Personally, I find a lot of fields to be a lot of visual clutter.] But the database should not have separate fields.

S.Lott
I do agree with the validation part which becomes hard to manage, if they are managed separately
Samuel
@Samuel: not "hard": "impossible". Date calculations are really hard to get right. Attempting date calculations outside the DB data methods is so hard to get correct that I prefer to call it "impossible".
S.Lott
+2  A: 

It depends on how you intend to use the data. If you will be simply displaying it or if you need to figure out the age of someone from the date, store in one date field. If you need to to see who has a birthday in May, then the separate fields might work better. If you need to do both, then store both ways though calcuated fields top avoid the cost of having to scrape out parts of the the date continually.

HLGEM
Even if I have to find all birth dates in the month of May, wouldn't it be possible if perform a query on the combined date field?
Samuel
appreciate your inputs on my comment
Samuel
Oh yes, it's possible, but if you do it frequently you have to use a date function to find it and if you have a separate calculated field, you only have to use the date function at the time of insert or update and not every time you query.
HLGEM
+1  A: 

It's easier to code the page if you do it as separate fields. But it's definitely better to do them as a single entity - better user experience.

You could use either representation in the DB. I personally would probably combine them back into a single date, so that all the related data was encapsulated in as small a unit as possible.

Anything that you need to check (like the birthday example above) can be done easily when stored as a single date field as well.

JGB146