问题
I have a serialized code, and from within this code there are numeric values which when parsed represent a date.
For example, 011756420176654
*Note* array index may be off
Substring(1,2) = 01
Substring(3,2) = 17
I'm trying to ignore the row, without replacing the original row. I have a derived column, and am doing this in the column.
(dt_date)(Substring([My Code], 1, 2) + "-" + Substring([My Code], 3, 2) + (dt_str,10,1252)datepart("year",getdate()))
My intention here is to configure my error output to ignore the [My Code] field if the "TryParse"-logic in the derived column fails. I know if I were passing the derived column, that selecting ignore on the configuration will pass null, but the problem is I'm trying to (on error) ignore the source row and pass that as null (ie [My Code]).
Once this hit's the database, another process consumes it and attempts to parse the dates. It will not fail on null values, so I want to validate that essentially "is date"-logic before allowing the record through, or setting it to null.
Edit: Per Keith's solution, I came to this. I was having issues accessing the output buffer, but after some MSDN on syntax I came up with the following which works perfectly.
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;
}
}
回答1:
I'd use a script transformation:
Add an ouput column (convDate) and check [My Code] as read:
Here's your 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; }
回答2:
It is better (from performance perspective) to avoid using Script Component while using simple expression
- SSIS component vs C# script performance
The problem that you have in you derived column expression is that you are missing the second dash -
and specify50
as length of string, try using the following expression:
(dt_date)(Substring([My Code], 1, 2) + "-" + Substring([My Code], 3, 2) + "-" + (dt_str,50,1252)datepart("year",getdate()))
Also it is preferable to use the universal date formatting YYYY-MM-DD
while handling date values:
(dt_date)((dt_str,50,1252)datepart("yy",getdate()) + "-" + Substring([My Code], 3, 2) + "-" + Substring([My Code], 1, 2))
Make sure you have configured the error output to ignore failure:
Update 1
If you need to return the original string value use the following expression:
((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)
来源:https://stackoverflow.com/questions/55184287/tryparse-ssis-ignore-source-row