问题描述
我正在使用sql server和excel上的日期和浮点转换。
I'm working with date and float conversion on sql server and excel.
这是我的查询:
select getdate(),convert(float, getdate())
我得到:
2014-11-21 16:38:49.973 41962,6936339506
如果我将此浮点数复制到Excel,并且将单元格类型更改为日期,则会得到以下值:
If I copy this float number to Excel and I change cell type as date I get this value:
19-nov-2014
那是什么?为什么要花两天时间?
What's that? Why there is an offest of two days?
推荐答案
SQL Server只是将日期时间转换为浮点数作为数字1900年1月1日午夜以来的天数(即 select convert(DATETIME,0)
给出 1900-01-01 00:00:00.000
)
SQL server simply calculates the conversion of a date time to a float as the number of days since midnight on 01-Jan-1900 (i.e. select convert(DATETIME, 0)
gives 1900-01-01 00:00:00.000
)
Excel计算出一个相似的数字,但零日期为 00/01/1900。这可能与excel使用基于1的索引而不是更常见的基于0的索引的事实有关。差异的第二天来自,因此excel认为1900年是a年。
Excel calculates a similar number, but the zero date is "00/01/1900". This is probably related to the fact that excel uses one based indexing, rather than the more common zero based indexing. The second day of difference comes from a well known bug whereby excel considers 1900 to have been a leap year.
外带消息:如果您认为excel总是落后两天,您将好的,除了1900年2月28日或之前的日期。
Takeaway message: if you assume that excel is always behind by two days you'll be ok, except for dates on or before the 28th of February 1900.
这篇关于SQL Server和Excel浮动至日期偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!