TryParse SSIS 忽略源行

TryParse SSIS Ignore Source Row

我有一个序列化代码,在这段代码中有一些数值,在解析时代表一个日期。

For example, 011756420176654 
*Note* array index may be off
Substring(1,2) = 01
Substring(3,2) = 17

我试图忽略该行,而不替换原始行。我有一个派生列,我正在列中执行此操作。

(dt_date)(Substring([My Code], 1, 2) + "-" + Substring([My Code], 3, 2) + (dt_str,10,1252)datepart("year",getdate()))

我在这里的目的是配置我的错误输出以在派生列中的 "TryParse" 逻辑失败时忽略 [My Code] 字段。我知道如果我正在传递派生列,那么在配置上选择忽略将传递 null,但问题是我试图(出错时)忽略源行并将其作为 null 传递(即 [我的代码])。

一旦此命中成为数据库,另一个进程就会使用它并尝试解析日期。它不会在 null 值上失败,所以我想在允许记录通过或将其设置为 null 之前验证本质上 "is date" 逻辑。

编辑:根据 Keith 的解决方案,我想到了这个。我在访问输出缓冲区时遇到问题,但在一些 MSDN 语法之后,我想出了以下完美运行的方法。

public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        DateTime dateValue;
        string test = Row.ReceiptCode.Substring(0, 2) + "/" + Row.ReceiptCode.Substring(2, 2) + "/" + DateTime.Now.Year.ToString();

        if (DateTime.TryParse(test, out dateValue) && Row.ReceiptCode.Length ==16)
    {

        Output0Buffer.AddRow();

        Output0Buffer.EndDate = Row.EndDate;
        Output0Buffer.Q10 = Row.Q10;
        Output0Buffer.Q8 = Row.Q8;

        Output0Buffer.ValidatedReceipt = Row.ReceiptCode;
    }
    else 
    {
        Output1Buffer.AddRow();

        Output1Buffer.EndDate = Row.EndDate;
        Output1Buffer.Q10 = Row.Q10;
        Output1Buffer.Q8 = Row.Q8;
        Output1Buffer.Error = Row.ReceiptCode;
    }
}

我会使用脚本转换:

添加一个输出列(转换日期)并检查[My Code] as read:

这是您的代码:

string test = Row.[My Code].Substring(1,2) + "/" + Row.[My Code].Substring(3,2)+"/" + DateTime.Now.Year.ToString();

if (DateTime.TryParse(test, out dateValue))
{Row.convDate = dateValue;  }

最好(从性能角度)在使用简单表达式时避免使用脚本组件

  • SSIS component vs C# script performance

您在派生列表达式中遇到的问题是您缺少第二个破折号 - 并指定 50 作为字符串的长度,请尝试使用以下表达式:

(dt_date)(Substring([My Code], 1, 2) + "-" + Substring([My Code], 3, 2) + "-" +  (dt_str,50,1252)datepart("year",getdate()))

此外,在处理日期值时最好使用通用日期格式 YYYY-MM-DD

(dt_date)((dt_str,50,1252)datepart("yy",getdate()) + "-" + Substring([My Code], 3, 2) + "-" + Substring([My Code], 1, 2))

确保您已将错误输出配置为忽略失败:


更新 1

如果需要return原始字符串值使用下面的表达式:

((dt_date)((dt_str,50,1252)datepart("yy",getdate()) + "-" + Substring([My Code], 3, 2) + "-" + Substring([My Code], 1, 2)) == (dt_date)((dt_str,50,1252)datepart("yy",getdate()) + "-" + Substring([My Code], 3, 2) + "-" + Substring([My Code], 1, 2))) 
? [My Code] 
: NULL(dt_str,50,1252)