简体   繁体   English

使用SSIS以不同方式填充从CSV到表的日期列

[英]Date column populating from CSV to table behaving differently using SSIS

I have CSV file 我有CSV文件

**date column**
06/04/1999
06/04/2000
.
.
.06/04/2017

I'm just loading this file from Csv file to Database table using SSIS 我只是使用SSIS将此文件从Csv文件加载到数据库表

Flat file source to OLEDB Destination 平面文件源到OLEDB目标

where date column is absolutely DT_STR and SQL Table date column is varchar(50) 其中date列绝对是DT_STR,SQL表日期列是varchar(50)

But it is behaving differently when I load sometimes it is populating like 但是当我加载时,它的行为会有所不同

  **date column**
    06/04/1999
    06/04/2000

and some other times like 和其他一些时间一样

 **date column**
    1999-04-06
    2000-04-06

I'm just trying to load the same what ever is there in Flat file but why it is giving "-" sometimes . 我只是想尝试加载平面文件中的相同内容,但为什么它有时会给出“ - ”。 Can any one please suggest me why it behaving differently each time . 任何人都可以建议我为什么每次都表现不同。 It doesn't have any conversions(like Derived Columns) in SSIS 它在SSIS中没有任何转换(如派生列)

You have to convert these values to DateTime then to String with desired format yyyy-MM-dd using a ScriptComponent Transformation 您必须使用ScriptComponent Transformation将这些值转换为DateTime,然后转换为具有所需格式的字符串yyyy-MM-dd

  1. Create a Flat File connection manager and an OLEDB Connection Manager (Source and Destination) 创建Flat File connection managerOLEDB Connection Manager (源和目标)
  2. Add a DataFlow Task 添加DataFlow Task
  3. In the DataFlow Task add a Flat File Source , Script Component , OLEDB Destination DataFlow Task添加Flat File SourceScript ComponentOLEDB Destination
  4. your DataFlow should look like Source --> Script --> Destination 您的DataFlow应该看起来像Source --> Script --> Destination
  5. In the Script component Mark the Time column as input( assuming it's name is inDateColumn ) and add an Output Column (ex: OutDateColumn ) of String DT_STR 在“脚本”组件中将“时间”列标记为输入(假设其名称为inDateColumn )并添加字符串DT_STR的输出列(例如: OutDateColumn
  6. In the Script Write The Following Code: (using Vb.net) 在脚本中编写以下代码:( 使用Vb.net)

     Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) If Not Row.inDateColumn_IsNull AndAlso _ Not String.IsNullOrEmpty(Row.inDateColumn.Trim) Then ' You can add more formats inside the following method Row.OutDateColumn = DateTime.ParseExact(Row.inDateColumn.Trim,New String(){"yyyy-MM-dd","dd/MM/yyyy"},New System.Globalization.CultureInfo("En-GB"), System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd") Else Row.OutDateColumn_IsNull = True End If End Sub 
  7. Map OutDateColumn to the destination column OutDateColumn到目标列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM