2017-05-18 54 views
2

我有以下信息,我正在試圖將其加載到具有相同的列和數據類型爲日期SQL Server表平面文件 -保留NULL不工作的所有列

StartDate,EndDate 
2017-01-02,2017-03-01 

2017-01-02,2017-03-01 

注意,第二行是空白的。現在我將源列設置爲DT_DBDATE數據類型。也在平面文件源我檢查選項Retain null values from the source as null values in the data flow

在目的地DFT我啓用Keep NULL選項。 這是問題所在。當我運行我看到空包的起始日期但結束日期我看到下面的錯誤 -

Error:Year, Month, and Day parameters describe an un-representable DateTime.

任何想法如何渡過這還是什麼原因造成這個問題?

DataViewer output

+0

日期在平面文件是一個巨大的痛苦的工作。我會推薦作爲文本分段到中間表,然後在插入到真正表中時重新格式化。可能爲你節省麻煩。不顯示問題的原因,但至少你有一個快速的解決方法。 –

+2

它期待兩個字段,但它將空白行讀作一個字段(因爲沒有逗號),所以EndDate甚至沒有NULL,它只是缺少。 – Donnie

+0

@Donnie - 感謝您指出這一點,您說得對,該行沒有必要的分隔符。讓我試試看分隔符。 – VKarthik

回答

1

的問題

空行不包含任何列分隔符這意味着它僅包含一列,而從FALT文件中讀取,這將導致問題。

如何解決它

在平面文件連接管理器中刪除列分隔符和讀每一行視爲DT_STR類型的一列,並設置長度4000

enter image description here

然後添加腳本組件並執行以下操作:

  1. 檢查該行是空
    • 如果是輸出空列
    • 如果不拆行成列
  2. 輸出必須包含您需要

enter image description here

您可以使用類似的代碼:(我用vb.net)

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) 

     If Not Row.Column0_IsNull AndAlso 
        Not String.IsNullOrEmpty(Row.Column0) Then 

      Dim strcolumnms() As String = Row.Column0.Split(CChar(";")) 

      Dim dtTemp As Date 


      If DateTime.TryParse(strcolumnms(0), System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then 

       Row.Column1 = dtTemp 

      Else 

       Row.Column1_IsNull = True 

      End If 


      If DateTime.TryParse(strcolumnms(1), System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then 

       Row.Column2 = dtTemp 

      Else 

       Row.Column2_IsNull = True 

      End If 


     Else 

      Row.Column1_IsNull = True 
      Row.Column2_IsNull = True 





     End If 

    End Sub 

在平面文件

這是更好地存儲在一個平面文件的日期值(或其他數據類型)工作時選擇DT_STR類型,並將其轉換(檢查後,如果他們也形成了日處理)使用Script ComponentData Conversion轉換。

使用腳本組件

首先,你必須添加Script component,標註日期列作爲輸入,創建類型DT_DBDATE

的一個新的輸出列。如果你想檢查特定日期格式您可以使用DateTime.TryParseExact()方法或使用DateTime.TryParse()來嘗試基於CultureInfo日期格式的分析日期。

我給兩個例子: 假設輸入列名是inDate,輸出列是outDate

DateTime.TryParseExact()

Dim strFormats() As String = {"yyyy-MM-dd"} 

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) 



    If Not Row.inDate_IsNull AndAlso 
       Not String.IsNullOrEmpty(Row.inDate) Then 


     Dim dtTemp As DateTime 

     If DateTime.TryParseExact(Row.inDate, strFormats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then 

      Row.outDate = dtTemp 

     Else 

      Row.outDate_IsNull = True 

     End If 



    End If 
End Sub 

日期時間.TryParse()

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) 



    If Not Row.inDate_IsNull AndAlso 
       Not String.IsNullOrEmpty(Row.inDate) Then 


     Dim dtTemp As DateTime 

     If DateTime.TryParse(Row.inDate, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtTemp) Then 

      Row.outDate = dtTemp 

     Else 

      Row.outDate_IsNull = True 

     End If 



    End If 
End Sub 

你可以在這個崗位使用SSIS的腳本組件閱讀更多關於日期時間的轉換:

使用數據轉換轉換

使用數據轉換組件onent到DT_STR列轉換爲DT_DBDATE並在Error Output設置不能轉換被替換Ignore failure所以通過數值NULL

enter image description here

+1

感謝您的更新,我完全同意在一個平面文件中處理日期是非常煩人的。最好通過數據轉換或派生列檢查空白,這是我所做的,我在這裏博客 - vankadarakarthik.com。唐尼指出了一個沒有定界符本身的非常恰當的觀點。我會一次運行,看看是否有幫助 – VKarthik

+0

@VKarthik我同意你,請檢查我的答案更新 – Hadi

+1

感謝您的努力和更新的答案。欣賞它。 – VKarthik