In the following question, field and table names have been changed to protect their identities.
If I have two database columns:
MONKEY_DATE DATETIME NULL (with data e.g. 2012-05-14 00:00:00.000)
MONKEY_TIME DATETIME NULL (with data e.g. 1753-01-01 16:30:53.025)
The date component of the time field is mostly set to 1st Jan 1753... but some data has 1st Jan 1899 and some has 1st Jan 1900.
I find that maintaining the code to query and report on these columns causes me (and our team) a headache that could easily be solved by merging the two columns. However, experience (and Terry Goodkind) has taught me that nothing is ever easy. See below some examples of why this is a headache.
My Approach
I'm thinking the following approach will have the desired effect of merging the two columns:
- Use SQL to update the data, setting the value for the date field and the value for the time field both to the same value, which is a mix of the date component from the date field and the time component from the time field
- Write any new code only using the MONKEY_DATE field
- Eventually phase out the MONKEY_TIME field and any of the date/time component SQL (see examples)
- Drop MONKEY_TIME
This will mean we don't immediately have to go and make retrospective changes to the whole system... all existing code will continue to work... and we can begin to do things The Right Way.
SQL for #1 might be (Oracle):
UPDATE MONKEY SET
MONKEY_DATE = TO_DATE(TO_CHAR(MONKEY_DATE, 'MM/DD/YYYY ') ||
TO_CHAR(MONKEY_TIME, 'HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS')
MONKEY_TIME = TO_DATE(TO_CHAR(MONKEY_DATE, 'MM/DD/YYYY ') ||
TO_CHAR(MONKEY_TIME, 'HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS')
The Question
My questions to you are:
- Should these fields be merged?
- Is my approach reasonable to merge these two columns?
- Do you think it would be better to skip steps two and three?
- Do you have any other (constructive) comments or suggestions?
Examples
For example, to select all my monkey dates and times and order them by date and time, I need to do something like this (SQL Server):
SELECT
CONVERT(DATETIME, CONVERT(VARCHAR, MONKEY_DATE, 101), 101) AS MONKEY_DATE
, CONVERT(DATETIME, CONVERT(VARCHAR, MONKEY_TIME, 108), 108) AS MONKEY_TIME
FROM MONKEY
ORDER BY
CONVERT(DATETIME, CONVERT(VARCHAR, MONKEY_DATE, 101), 101) DESC
, CONVERT(DATETIME, CONVERT(VARCHAR, MONKEY_TIME, 108), 108) DESC
or this (Oracle - slightly more explicit):
SELECT
TO_DATE(TO_CHAR(MONKEY_DATE, 'MM/DD/YYYY'), 'MM/DD/YYYY') AS MONKEY_DATE
, TO_DATE(TO_CHAR(MONKEY_TIME, 'HH24:MI:SS'), 'HH24:MI:SS') AS MONKEY_TIME
FROM MONKEY
ORDER BY
TO_DATE(TO_CHAR(MONKEY_DATE, 'MM/DD/YYYY'), 'MM/DD/YYYY') DESC
, TO_DATE(TO_CHAR(MONKEY_TIME, 'HH24:MI:SS'), 'HH24:MI:SS') DESC
I also often find myself selecting a merged date/time column (Oracle):
SELECT
TO_DATE(TO_CHAR(MONKEY_DATE, 'MM/DD/YYYY ') ||
TO_CHAR(MONKEY_TIME, 'HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS') AS MONKEY_DATE_TIME
FROM MONKEY
Because, almost all the time, we want to know the date and time of the monkey.
The above SQL could be easily altered to:
SELECT MONKEY_DATE_TIME FROM MONKEY ORDER BY MONKEY_DATE_TIME
... If only we had merged columns.
Background
I've inherited an old ASP system that stores dates and times in separate columns in the database. I've been told this is probably because the application started out in an early version of Access, where it was not possible to store both date and time in the same column. The whys and hows aren't really part of this question, but some people like to know.
P.S.
I really nearly posted this in SO.SE, so my apologies if I got the wrong site.