在我的 SSIS 包中,我必须将值从 DateTime 转换为相应的 INTEGER 值。已提供以下示例。
关于如何转换这些有什么想法吗?
DATETIME INT
--------- ----
1/1/2009 39814
2/1/2009 39845
3/1/2009 39873
4/1/2009 39904
5/1/2009 39934
6/1/2009 39965
7/1/2009 39995
8/1/2009 40026
9/1/2009 40057
10/1/2009 40087
11/1/2009 40118
12/1/2009 40148
1/1/2010 40179
2/1/2010 40210
3/1/2010 40238
4/1/2010 40269
5/1/2010 40299
6/1/2010 40330
最佳答案
编辑:在最新版本的 SQL Server 中,转换为 float/int 不再有效。请改用以下内容:
select datediff(day, '1899-12-30T00:00:00', my_date_field)
from mytable
请注意,字符串日期应位于 unambiguous date format 中这样它就不会受到服务器区域设置的影响。
<小时/>在旧版本的 SQL Server 中,您可以通过先转换为 float ,然后再转换为整数,将 DateTime 转换为 Integer:
select cast(cast(my_date_field as float) as int)
from mytable
(注意:您不能直接转换为 int,因为如果您过了中午,MSSQL 会将值向上舍入!)
如果您的数据中存在偏移,您显然可以从结果中添加或减去该偏移
您可以通过直接向后转换来向另一个方向进行转换:
select cast(my_integer_date as datetime)
from mytable
关于sql-server - 从日期时间转换为 INT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505935/