I have inherited a MySQL database which (for historical reasons) contains a field which stores a date as number of days since 1900-01-01. I want to be able to select a date_format() version of this date but can't figure a way of doing so.
Is there a way of manipulating the contents of the column such that I can pass the manipulated value to date_format() so it is recognised as a valid date?
I realise there is a solution to this which involves doing one query, then manipulating the field (in PHP) then passing back in as a secondary query - but I need this to be a single query solution.
2 Answers
Is the MySql FROM_DAYS() function what you're looking for - see here for docs?
However, depending on your database design and use case, you might consider adding a column with the date (result of FROM_DAYS()), so that you can do queries against that column. When you have to apply a function to a column before being able to use this in a query, it should ring warning bells that these queries could be quite inefficient to run.
1 Comment
Can you create views and functions on the database? If so I would create a function with the query you are currently using to convert the time and then create a view using that function to display the time as a timestamp. This will improve your efficiency and make your php code cleaner :)