3

我有一个序列化代码,并且在此代码中存在解析时表示日期的数值。

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(即 [我的代码])。

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

编辑:根据基思的解决方案,我来到了这个。我在访问输出缓冲区时遇到问题,但是在一些 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;
    }
}
4

2 回答 2

3

我会使用脚本转换:

添加一个输出列 (convDate) 并检查 [My Code] 为:

这是你的代码:

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;  }
于 2019-03-15T14:27:08.613 回答
2

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

您在派生列表达式中遇到的问题是您缺少第二个破折号-并指定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

如果您需要返回原始字符串值,请使用以下表达式:

((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)
于 2019-03-15T20:30:59.607 回答