我有一个 SSIS 包,可以将一些数据从 Oracle 传输到 SQL Server。
在 Oracle 中,日期存储为浮点型,例如42824 == '2017-04-01'
- 使用数据库的应用程序是用 Delphi 编写的。
当选择CAST(42824作为日期时间)
在 Management Studio 结果 '2017-04-01 00:00:00.000'
中,由包插入 SQL Server 表中的日期时间列的相同值 (42824) 显示 2017-03-30 00:00:00.000
。
注意:此数字的源数据类型为 DT_R8
,在数据转换组件中将类型更改为 DT_UI4
不会发生任何变化
谁能解释一下吗?
最佳答案
关于日期序列
存储在 Oracle 中的值 (42824
) 称为日期序列,它也用于 Microsoft Excel
中。
Date Serial 表示日期值与初始值 1899-12-30
之间的天数
您可以在以下位置阅读有关日期连续剧的更多信息:
- Why is 1899-12-30 the zero date in Access / SQL Server instead of 12/31?
- convert Excel Date Serial Number to Regular Date
CAST方法
来自Microsoft Docs - CAST and CONVERT (Transact-SQL) :
Only supported when casting from character data to datetime or smalldatetime. When character data that represents only date or only time components is cast to the datetime or smalldatetime data types, the unspecified time component is set to 00:00:00.000, and the unspecified date component is set to 1900-01-01
因此,CAST
函数在转换日期时将值 1900-01-01
视为初始值。所以我们在使用它转换Date Serials时需要减去2天
有 2 种方法可以使用 SQL Server 将其转换为日期:
select DATEADD(d,42824,'1899-12-30')
select CAST(36464 - 2 as SmallDateTime)
SSIS 隐式转换
DBTYPE_DATE (This is an automation DATE type. It is internally represented as a double.. The whole part is the number of days since December 30, 1899 and the fractional part is the fraction of a day. This type has an accuracy of 1 second, so has an effective scale of 0.)
因此,SSIS 中的隐式转换在转换日期时将值 1899-12-30
视为初始值。所以用它转换Date Serials时不需要减去2天
关于sql - CAST 与 ssis 数据流隐式转换差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43635787/