MySQL code to convert Excel datetime

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-16 20:15:47

问题


Excel's datetime values look like 42291.60493, which means MySQL sees them as strings and not as dates. Is there a MySQL code that can convert them to MySQL datetime? (i.e. like in MS SQL)


回答1:


I can think of 2 solutions:

  1. Convert your dates within excel to a formatted date string that conforms to mysql's date and time format using text() function within excel.

  2. Convert the number using calculation to date within mysql:

(the expression below may be simplified)

select date_add(date_add(date('1899-12-31'), interval floor(@datefromexcel) day), interval floor(86400*(@datefromexcel-floor(@datefromexcel))) second)



回答2:


Excel stores date times as the number of days since 1899-12-31. Unix stores the number of seconds since 1970-01-01, but only non-negative values are allowed.

So, for a date, you can do

select date_add(date('1899-12-31'), interval $Exceldate day )

This doesn't work for fractional days. But, for a unix date time, this would be nice:

select $ExcelDate*24*60*60 + unix_timstamp('1899-12-31')

But negative values are problematic. So, this requires something like this:

select ($ExcelDate - datediff('1899-12-31', '1970-01-01')) * 24*60*60

That is, just count the number of seconds since the Unix cutoff date. Note: this assumes that the date is after 1970-01-01, because MySQL doesn't understand unix dates before the cutoff.



来源:https://stackoverflow.com/questions/33125037/mysql-code-to-convert-excel-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!