将vb转换从dts转换为ssis时输出空值

时间:2014-04-09 15:45:20

标签: sql sql-server vb.net vbscript ssis

我正在努力将一些使用ActiveX VBScript转换的旧dts包转换为SSIS包。输入是YYYYMMDD格式的8个字符的字符串。如果可能,转换将其转换为日期。如果不是,则该值应为NULL。

这是dts代码

TheYear = Mid(DTSSource("SHIP_DATE"),1,4)
TheMonth = Mid(DTSSource("SHIP_DATE"),5,2)
TheDay = Mid(DTSSource("SHIP_DATE"),7,2)
TheDate = TheMonth &"/"&TheDay&"/"&TheYear

If IsDate(TheDate) Then
    DTSDestination("SHIP_DATE") = TheDate
End If

以下是我对SSIS转型的看法

Dim TheYear As String
Dim TheMonth As String
Dim TheDay As String
Dim TheDate As String

If Row.SHIPDATE.Length >= 8 Then
    TheYear = Row.SHIPDATE.Substring(0, 4)
    TheMonth = Row.SHIPDATE.Substring(4, 2)
    TheDay = Row.SHIPDATE.Substring(6, 2)
    TheDate = TheMonth & "/" & TheDay & "/" & TheYear
    If IsDate(TheDate) Then
        Row.OutShipDate = TheDate
    Else
        Row.OutShipDate = Nothing
    End If
Else
    Row.OutShipDate = Nothing
End If

我尝试将OutShipDate格式化为日期和数据库日期。对于无效的日期输入字符串,例如" 00000000",我在数据库列中获得1899-12-30或0001-01-01,具体取决于OutShipDate的dataype。如果我没有将Row.OutShipDate设置为VBScript中的任何内容,则SSIS执行将完全失败。

我可以从VBScript转换中输出空值吗?

2 个答案:

答案 0 :(得分:0)

你不应该转换为日期......

Row.OutShipDate = CDate(TheDate)

DB列是否设置为允许NULL?

更新

If IsDate(TheDate) Then
    Row.OutShipDate = CDate(TheDate)
Else
    Row.OutShipDate = DBNull.value
End If

另一次更新......

If IsDate(TheDate) Then
    Row.OutShipDate = DateTime.Parse(TheDate("MM/dd/yyyy"))
Else
    Row.OutShipDate = DirectCast(Nothing, System.Nullable(Of DateTime))
End If

这对我有用......

Dim TheDate As String = "36/29/2014"
'Dim TheDate As String = "4/9/2014"    

    Dim DDB As Date

    If IsDate(TheDate) Then
        DDB = CDate(TheDate)
    Else
        DDB = Nothing
    End If

答案 1 :(得分:0)

我找到了答案,现在我知道它是什么,它看起来很简单。

基本上,如果我想返回一个NULL日期,代码应该是

Row.OutShipDate_IsNull = True

This link helped ultimately