sql - CAST 与 ssis 数据流隐式转换差异

标签 sql sql-server oracle ssis etl

我有一个 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 之间的天数

您可以在以下位置阅读有关日期连续剧的更多信息:

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 隐式转换

也按照这个Microsoft docs article

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/

相关文章:

sql - 在不损失精度的情况下将 SQL Geography Lat/Long 转换为 VARCHAR

mysql非字母顺序?

sql - 导入时自动调整数据类型

java - 了解典型 Java Web 应用程序中的字符编码

oracle - 更改过程包和包体 PL/SQL

sql - 适用于 .Net 的良好嵌入式数据库解决方案(如 SQLite)

sql - 如果子查询在 Postgres 中返回多行,则跳过行

c# - 如何使用 EF6 和 SQL Server 捕获 UniqueKey Violation 异常?

Sql 查询查找相关行中列的总和

Oracle DBMS_SESSION SET_CONTEXT 不存储值