sql - 将存储为 DT_STR 的日期转换为 DT_DBDATETIMEOFFSET

标签 sql sql-server datetime ssis etl

我有带分隔符的文本文件,我正在将其加载到数据库表中 如何将 DT_STR col 加载到 DT_DBDATE,将 DT_STR 加载到 DT_DATETIME。

enter image description here

在文本文件中

COL1 : Predicted delivery date : DT_STR
COL2  : ScanDateTime : DT_STR

enter image description here

在目标表中:

COL1 : Predicted delivery date : DATE  (DataType)
COL2  : ScanDateTime : DATETIME 

我需要以下面的格式加载数据

Switchoffset (Substring(ScanDateTime , 1, 22)+':'+Substring(ScanDateTime , 23, 24),'-05:00')

我尝试使用派生列将此数据加载到目标表中:

我给了这个表达式

第 1 列:

(DT_DBDATE)LEFT([Predicted Delivery Date],10)

COL 2:

(DT_DATE)(SUBSTRING([ScanDateTime ],1,4) + "-" + SUBSTRING([ScanDateTime ],5,2) + "-" + SUBSTRING([ScanDateTime ],7,2),'-05:00')

但两者都给出错误:

[Derived Column 2] Error: An error occurred while attempting to perform a type cast. [Derived Column 2] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "Derived Column" failed because error code 0xC0049064 occurred, and the error row disposition on "Derived Column.Outputs[Derived Column Output].Columns[Predicted]" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure.

对于 Col1,我需要如何处理空值并将数据加载到日期格式的目标表中?

对于 Col2,我如何为 Switchoffset (exp) 编写表达式 - 5 小时用于该日期时间列并加载?

最佳答案

预计交货期Null处理

您可以使用以下表达式添加派生列:

(ISNULL([Predicted delivery date]) || [Predicted delivery date] == "") ? 
NULL(DT_DATE) : 
(DT_DATE)[Predicted delivery date]

ScanDateTime 偏移处理

您可以通过添加脚本组件转换来解决问题,选择 ScanDateTime 作为输入列并添加名为 outDate 的类型为 DT_DBDATETIMEOFFSET 的新列,在脚本中使用以下代码:

public override void Input0_ProcessInputRow(Input0Buffer Row) 
{ 
    if (!Row.ScanDateTime_IsNull && !String.IsNullOrEmpty(Row.ScanDateTime)){

        DateTime dtDate = DateTime.Parse(Row.ScanDateTime);
        dtDate = DateTime.SpecifyKind(dtDate, DateTimeKind.Unspecified);
        DateTimeOffset offDate = new DateTimeOffset(dtDate,
                               TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time").GetUtcOffset(dtDate));

        Row.outDate = offDate;

    }else{

        Row.outDate_IsNull = True;

    }
}

引用资料

关于sql - 将存储为 DT_STR 的日期转换为 DT_DBDATETIMEOFFSET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206360/

相关文章:

sql - Oracle:转置表

mysql - 更新mysql中的查询与mssql中的结果不一样

sql-server - AWS SQL Server 2017 RDS 上的 SQLCLR 支持

sql-server - IFilters 2.0 能否对Office 2007 文档中图表中的文本进行全文索引?

c++ - 在 C++ 中以 mm/dd/yyyy 获取当前时间

c# - 将日期时间重新格式化为 2014 年 11 月 7 日

mysql - 多个日期时间的mysql SUM问题

sql - 对于单个应用程序来说拥有多个 SQL 数据库是一个坏主意吗?

mysql - SQL——多对多查询

sql - 如何计算表格中的持续时间