[英]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.