简体   繁体   English

如何将日期从文件加载到SQL Server表中以及如何处理SSIS中的其他字符

[英]How to load date from a file into SQL server table and also handle other characters in SSIS

I am new to SSIS and I am trying to import a CSV file into a SQL server table using SSIS. 我是SSIS的新手,正在尝试使用SSIS将CSV文件导入到SQL Server表中。 The CSV file has a column for date and I have set the data type as DT_DATE . CSV文件的日期列,我将数据类型设置为DT_DATE The format of the date MM/dd/yyyy . 日期MM/dd/yyyy的格式。 However, for months and dates less than 10, it does not have the leading zeroes, ie, March 7, 2012 is in the file 3/7/2012 . 但是,对于小于10的月份和日期,它没有前导零,即March 7, 2012在文件3/7/2012 The rows that do not have the date values has a - character. 没有日期值的行带有-字符。 Can someone help me, how do load this data into SQL server. 有人可以帮我吗,如何将这些数据加载到SQL Server中。 I assume the date of 3/7/2012 will be loaded into the table without issues. 我假设3/7/2012月7日的日期将3/7/2012地加载到表格中。 However, I want to handle the dashes and load them as NULL into the SQL server. 但是,我想处理破折号并将其作为NULL加载到SQL Server中。 Can I use a derived column? 我可以使用派生列吗? If the value is -, set it to NULL , other populate the date value as it is. 如果值为-,则将其设置为NULL ,其他值将按原样填充日期值。 Is this correct? 这个对吗?

I tried the below code and I am getting some error message. 我尝试了以下代码,但出现了一些错误消息。

ISNULL([Last Contacted Date]) || (DT_WSTR,1)[Last Contacted Date] == "-" || (DT_WSTR,0)[Last Contacted Date] == "" ? NULL(DT_DATE) : [Last Contacted Date]

For the Derived Last Contacted Date column, I had set the Error and Truncation properties to "Ignore failure" and the job ran fine. 对于“派生的上次联系日期”列,我已将“错误”和“截断”属性设置为“忽略故障”,并且作业运行良好。

However, when I run the SSIS job, the data is getting loaded into the SQL server table, but the last contacted date on ALL records in SQL server table is NULL. 但是,当我运行SSIS作业时,数据已加载到SQL Server表中,但是SQL Server表中所有记录的最后联系日期为NULL。 The file has several rows where the value is a valid date. 该文件有几行,其中的值是有效日期。

Can someone help? 有人可以帮忙吗?

In the Flat File Connection Manager, set the column type to DT_WSTR with an appropriate length, then use a Derived Column with the following expression: 在平面文件连接管理器中,将列类型设置为具有适当长度的DT_WSTR ,然后使用具有以下表达式的派生列:

REPLACENULL([Last Contacted Date],"") == "" ? NULL(DT_DATE) :
(DT_DATE)(RIGHT("0" +  [Last Contacted Date], FINDSTRING( [Last Contacted Date], "/", 1 ) - 1) 
+ "/" + 
RIGHT("0" + SUBSTRING([Last Contacted Date], FINDSTRING( [Last Contacted Date], "/", 1 ) +1, FINDSTRING( [Last Contacted Date], "/", 2 ) - FINDSTRING( [Last Contacted Date], "/", 1 ) - 1), 2)
+ "/" + 
RIGHT([Last Contacted Date],4))

暂无
暂无

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

相关问题 将数据从平面文件加载到 Sql 服务器表,并使用 SSIS 导出到 excel - Load data from flat file to Sql Server table and also export to excel using SSIS 有没有办法将 Excel 文件加载到 SSIS 中的 SQL 服务器表中? - Is there a way to load an Excel file into a SQL Server table in SSIS? 如何使用SSIS(从Web门户下载的文件)将XML Spreadsheet 2003(.xml)加载到SQL Server中 - How to load an XML Spreadsheet 2003(.xml) in to SQL server using ssis (file downloaded from web portal) 如何使用 SSIS 将 json 从 Azure Blob 加载到 SQL Server? - How to load json from Azure Blob to SQL Server using SSIS? 使用SSIS从平面文件源将特殊字符加载到ms sql时,特殊字符将转换为其他类型 - Special characters are converting into some other type while loading into ms sql from flat file source using SSIS 如何将xml文件加载到SQL Server表中 - How to load an xml file into a SQL Server table SSIS使用太多内存,无法将大型(40GB +)XML文件加载到SQL Server表中 - SSIS using too much memory to load large (40GB+) XML file into SQL Server table SSIS:日期格式向SQL Server表发出csv文件:DD和MM交换 - SSIS: Date format Issue csv file to a SQL Server table: DD and MM swapped 如何使用SSIS从sql表生成Excel文件? - How to generate an excel file from sql table using SSIS? 从 Onedrive 访问文件并使用 SSIS 包 2015 加载到 sql server 表 - Access the files from Onedrive and load in to sql server table using SSIS package 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM