简体   繁体   English

SSIS:日期格式向SQL Server表发出csv文件:DD和MM交换

[英]SSIS: Date format Issue csv file to a SQL Server table: DD and MM swapped

I have a csv file with a Date column with MM/DD/YYYY format. 我有一个带有MM / DD / YYYY格式的Date列的csv文件。 When I load the csv file to a SQL Server table with SSIS, I would like to have the following format: YYYY-MM-DD. 当我使用SSIS将csv文件加载到SQL Server表时,我希望具有以下格式:YYYY-MM-DD。

The issue is that when the csv file contain a date with a day value between 1 and 12, the day and the month are swapped. 问题是当csv文件包含日期值介于1和12之间的日期时,将交换日期和月份。

Example: 例:

12/06/2019 in csv file -> 2019-06-12 in SQL Server table: DD and MM are swapped
MM/DD/YYYY  ->  YYYY-MM-DD

How could I do to have 2019-12-06 in SQL Server table? 如何在SQL Server表中使用2019-12-06?

Thanks in advance for yours answers. 在此先感谢您的回答。

I don't see why that shouldn't work. 我不明白为什么不起作用。

I took an example: 我举了一个例子:

1. Created a table 1.创建一个表

CREATE TABLE [dbo].[DateTable](
[TestDat] [datetime] NULL,
[TestDat1] [datetime] NULL,
[TestDat2] [datetime] NULL,
[TestDat3] [datetime] NULL) ON [PRIMARY]


2. Created a CSV file with following dates: 2.创建了一个包含以下日期的CSV文件:
12/06/2019,01/06/2019,02/06/2019,03/06/2019 12月6日/ 2019,01 / 06 / 2019,02 / 06 / 2019,03 / 06/2019

  1. Created an SSIS package with only dataflow. 创建仅包含数据流的SSIS包。 The dataflow task contains a Flat File Source and an OLE DB Destination. 数据流任务包含平面文件源和OLE DB目标。
    The DataFlow task looks like this: DataFlow任务如下所示:
    在此输入图像描述
    And the mapping in OLE DB Destination looks like this: OLE DB Destination中的映射如下所示:
    在此输入图像描述

After running the SSIS package, I get the following data populated to SQL Server Table. 运行SSIS包后,我将以下数据填充到SQL Server表。
It is has always taken 06 as Date. 它始终以06为日期。
SQL表结果

This is a common issue, since when handling MM/dd/yyyy and dd/MM/yyyy date string formats and month and day are less than 12. 这是一个常见问题,因为处理MM / dd / yyyy和dd / MM / yyyy日期字符串格式以及月份和日期都小于12。

Since csv files are text files date are stored as text, implicitly converting these fileds to a datetime filed (sql database) may cause this issue. 由于csv文件是文本文件,因此日期存储为文本,隐式将这些文件转换为日期时间字段(sql数据库)可能会导致此问题。

Best way to insure that dates are converted correctly is to add a Script component, add a Output column of type DT_DATE (exampl: outDate ) and use DateTime.ParseExact() function to force converting using MM/dd/yyyy format: 确保正确转换日期的最佳方法是添加脚本组件,添加DT_DATE类型的输出列(例如: outDate )并使用DateTime.ParseExact()函数强制使用MM/dd/yyyy格式进行转换:

Row.outDate = DateTime.ParseExact(Row.csvDateFiled,"MM/dd/yyy",System.Globalization.CultureInfo.InvariantCulture);

You can refer to the following similar question for more details: 您可以参考以下类似问题以获取更多详细信息:

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

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