时间戳到 Bigint (MSSQL) 与 DT_BYTES 到 DT_I8 (SSIS)

Timestamp to Bigint (MSSQL) vs. DT_BYTES to DT_I8 (SSIS)

在处理来自 SQL 服务器的 "timestamp" 数据类型时,我在 SSIS 中进行正确的数据转换时遇到问题。

我有一个 table 是使用 SSIS 暂存的。在我的数据流任务 (DFT) 顶部的 source-select 中,它看起来像这样:

SELECT
     [SourceColumn1Timestamp]       = [SourceColum1Timestamp]
   , [SourceColumn2]               = [SourceColumn2]
   , [SourceColumn3]               = [SourceColumn3]
   , [SourceColumn4]               = [SourceColumn4]

FROM [dbo].[Table1]

我想将 [SourceColumn1Timestamp] 转换为 Bigint,但是 在 SSIS 中使用数据流任务中的数据转换组件。像这样:

因此 "DT_BYTES" SSIS 数据类型的输入列 "timestamp" 被转换为 DT_I8。

然而结果和我想象的完全不一样。与我在 SQL 中进行转换时得到的结果不同,将 DFT 中的 source-select 调整为:

SELECT
     [SourceColum1Timestamp]        = [SourceColum1Timestamp]
   , [SourceColumn2]                = [SourceColumn2]
   , [SourceColumn3]                = [SourceColumn3]
   , [SourceColumn4]                = [SourceColumn4]
   , [SourceColum1Timestamp_BIGINT] = CONVERT(BIGINT, [SourceColum1Timestamp]) -- I would like to avoid this
FROM [dbo].[Table1]

怎么会?

这里有一行显示了 SQL 转换和 SSIS 之间的区别: SQL:

SSIS:

如果您想在 SSIS 中获得与 T-SQL 相同的值,您需要在转换中反转二进制值的字节序列。这会将二进制值解释为 little-endian 序列(最低有效字节在前),因此该值将按预期转换为 64 位带符号整数。

编辑:

我不知道 SSIS 中是否有更好的方法,但您可以反转时间戳字节数组并在 C# 脚本转换中转换为 64 位整数。这是一个函数来代替现有的转换:

Int64 convertTimestampToInt64(byte[] timestampBytes)
{
    Array.Reverse(timestampBytes, 0, timestampBytes.Length);
    var timestampInt64 = BitConverter.ToInt64(timestampBytes, 0);
    return timestampInt64;
}

基于这个microsoft article,我认为timestamp可以隐式转换成BigInt