我在文本文件中有以下时间戳:
3/3/2008 15:21:33
使用SSIS派生列,我试图检查TimeStamp的格式是否正确。然后将其转换为:
2008-03-03 15:21:33.000
我收到以下错误:
Data conversion failed. The data conversion for column "TIME_STAMP"
returned status value 2 and status text "The value could not be
converted because of a potential loss of data.
如果转换失败或时间戳无效,我想插入Null。
我试过了,但它没有用。
(LEN(TRIM(TIME_STAMP)) < 23) || (TRIM(TIME_STAMP) == "")
|| (ISNULL(TIME_STAMP)) ? NULL(DT_DBTIMESTAMP) : (DT_DBTIMESTAMP)TIME_STAMP
如果时间戳失败转换,如何插入null,只有在格式有效时才转换?
更新:数据类型LEN
TRIM
和(DT_DBTIMESTAMP)
由于
答案 0 :(得分:1)
SSIS表达式不支持类型检查或异常处理,因此您在这些方面受到限制。下面的表达式将采用您的字符串并将其置于您想要的格式,但是,如果您的TIME_STAMP
列无法转换为DT_DATE
,则您的包将失败。例如,如果文本文件中的值为3/3/2008T15:21:33
,则会失败。
(DT_WSTR, 23)
(
(LEN(TRIM(TIME_STAMP )) < 17) || (TRIM(TIME_STAMP ) == "")
|| (ISNULL(TIME_STAMP )) ? NULL(DT_DBTIMESTAMP2, 3) : (DT_DBTIMESTAMP2, 3)
(DT_DATE)TIME_STAMP
)
您总是可以在C#中编写自定义脚本组件转换来执行此类验证/转换。代码的主要部分可能如下所示:
/// <summary>
/// This method is called once for every row that passes through the component from Input0.
///
/// Example of reading a value from a column in the the row:
/// string zipCode = Row.ZipCode
///
/// Example of writing a value to a column in the row:
/// Row.ZipCode = zipCode
/// </summary>
/// <param name="Row">The row that is currently passing through the component</param>
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (!Row.DerivedColumn1_IsNull)
{
DateTime dt;
if (DateTime.TryParse(Row.DerivedColumn1, out dt))
{
Row.DerivedColumn1 = dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
else
{
Row.DerivedColumn1_IsNull = true;
}
}
}