I've inherited a MySQL database with a date column type of Varchar
.
The column contains decimal strings. For example: 41143.646585648
.
How would I go about converting this into DateTime format?
All you need to know is the date the original developer defined as the value zero. Then it's a simple matter of adding the integer number of days to that date, then multiplying the fractional part by 86400 to get seconds and doing the arithmetic to determine the hour, minute and second.
If you have a row where you know the actual date and the stored value you can determine the zero date easily by subtraction.
You could string replace the dot, but the remaining count of numbers is to high for a unix timestamp. I don't know how the date was converted to decimal. Maybe you should split your string rather than string replace.
Maybe this code gives you some ideas for a solution:
#SELECT FROM_UNIXTIME(REPLACE('41143.646585648', '.', '')); # Does not work due to wrong amount of numbers in string
SELECT FROM_UNIXTIME(REPLACE('13577.77916', '.', '')); # works