【问题标题】:outputting null values when converting vb transformation from dts to ssis将vb转换从dts转换为ssis时输出空值
【发布时间】:2014-04-09 15:45:20
【问题描述】:

我正在努力将一些使用 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”,根据 OutShipDate 的数据类型,我在数据库列中得到 1899-12-30 或 0001-01-01。如果我没有将 Row.OutShipDate 设置为 VBScript 中的任何内容,则 SSIS 执行将完全失败。

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

【问题讨论】:

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


    【解决方案1】:

    如果你不转换到日期......

    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
    

    【讨论】:

    • 感谢您的建议。是的,DB 目标列允许 NULL。 CDate(TheDate) 导致包执行失败。
    • 好的,你能举几个 TheDate 作为字符串的例子吗?如果可能的话,好的和坏的。
    • 有效日期为 20140409 为 4/9/14,大部分无效日期为 00000000,我想为这些日期返回 Null
    • 呃...返回 Nullable 对象必须有一个值。在 System.Nullable`1.get_Value()。我想我会放弃并将其设置为 Nothing,然后执行单独的 sql 更新语句将所有“0001-01-01”设置为 NULL。不过,我感谢您的帮助。
    • @jessieloo 我刚刚遇到stackoverflow.com/questions/102084/hidden-features-of-vb-net,其中有一段关于 David T Macet 发送的可空日期...它在列表的下方,所以请继续滚动。
    【解决方案2】:

    我找到了答案,现在我知道它是什么了,这似乎很简单。

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

    Row.OutShipDate_IsNull = True
    

    This link helped ultimately

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多