简体   繁体   English

如何使用SSIS派生列转换为NULL日期分配默认值?

[英]How to assign a default value to NULL dates using SSIS derived column transformation?

In my SQL 2008 SSIS environment, I have data coming from a CSV file and I need to fix up null dates. 在我的SQL 2008 SSIS环境中,我有来自CSV文件的数据,我需要修复空日期。

Data from CSV looks like this: CSV中的数据如下所示:

01011990
01011990
01011990
01011990
01011990

In my staging table, I have a column defined as: 在我的临时表中,我有一个列定义为:

[SHIPPED DATE] date NOT NULL

First I fix the date values by pushing the column through a derived transformation that has: 首先,我通过将列推送到具有以下内容的派生变换来修复日期值:

(DT_DBDATE)(SUBSTRING([SHIPPED DATE],1,2) + "/" + SUBSTRING([SHIPPED DATE],3,2) + "/" + SUBSTRING([SHIPPED DATE],5,4)) (DT_DBDATE)(SUBSTRING([SHIPPED DATE],1,2)+“/”+ SUBSTRING([SHIPPED DATE],3,2)+“/”+ SUBSTRING([SHIPPED DATE],5,4))

The above transformations makes my string dates coming form CSV import into a date column in my SQL db. 上面的转换使我的字符串日期从CSV导入到我的SQL数据库中的日期列。

The output of this is: 这个输出是:

1990-01-01
1990-01-01
1990-01-01
1990-01-01
1990-01-01

Next , I'd like to deal with NULL dates so that I can stay true to the "NOT NULL" definition in my db. Next ,我想处理NULL日期,以便我可以保持我的数据库中的“NOT NULL”定义。 What I want to do is assign the NULL dates a default value but I get an error message as per below: 我想要做的是将NULL日期指定为默认值,但我收到如下错误消息:

Error at CSV to SQL [Derived Column [24817]]: The data types "DT_WSTR" and "DT_DBDATE" are incompatible for the conditional operator. CSV到SQL时出错[派生列[24817]]:数据类型“DT_WSTR”和“DT_DBDATE”与条件运算符不兼容。 The operand types cannot be implicitly cast into compatible types for the conditional operation. 操作数类型不能隐式转换为条件操作的兼容类型。 To perform this operation, one or both operands need to be explicitly cast with a cast operator. 要执行此操作,需要使用强制转换运算符显式转换一个或两个操作数。

My CSV column is WSTR, my destination in the SQL DB is a "date" column. 我的CSV列是WSTR,我在SQL DB中的目标是“日期”列。

My transformation I am trying to use is: ISNULL([SHIPPED_DATE]) ? 我试图使用的转换是:ISNULL([SHIPPED_DATE])? "1900-01-01" : [SHIPPED_DATE] “1900-01-01”:[SHIPPED_DATE]

It does not want to work due to the above message. 由于上述消息,它不想工作。 Can anyone point me to the right direction? 有人能指出我正确的方向吗?

Change your expression as shown below. 更改您的表达式,如下所示。 You have two parts to the ternary operator. 三元运算符有两部分。

Part 2 you have it as [SHIPPED_DATE] , which is of type DT_DBDATE because you are already casting this value in the derived column transformation. Part 2您将其作为[SHIPPED_DATE] ,它的类型为DT_DBDATE因为您已在派生列转换中转换此值。

However, Part 1 is simply assigned as text "1900-01-01" . 但是, Part 1只是指定为文本"1900-01-01" You need to add ( DT_DBDATE ) before that string value to type cast it to date format so that both the values specified on the ternary operator are of same data type. 您需要在该字符串值之前添加( DT_DBDATE )以将其转换为日期格式,以便在三元运算符上指定的值都具有相同的数据类型。

From

 ISNULL([SHIPPED_DATE]) ? "1900-01-01" : [SHIPPED_DATE]

To

 ISNULL([SHIPPED_DATE]) ? (DT_DBDATE)"1900-01-01" : [SHIPPED_DATE]

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

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