简体   繁体   English

如何从SSIS中的文本文件中将具有NULL值的列读取为NULL,而不必用随机的东西替换它

[英]how to read a column with NULL value from a text file in SSIS as NULL and not have to replace it with something random

Its a simple task, and I have tried a few things which aren't working, I am going to lay down all the trials here. 这是一个简单的任务,我已经尝试了一些不起作用的东西,我将在这里进行所有试验。

TASK: Take data from a text file delimited by ',' and move into an existing empty table in SQL Server. 任务:从以','分隔的文本文件中获取数据,然后移入SQL Server中现有的空表中。

  1. My source destination has the option "Retain null values...." checked. 我的源目标已选中“保留空值...”选项。
  2. There is no mismatch of data type between source and destination columns 源列和目标列之间没有数据类型不匹配
  3. I also have "Keep Nulls" option checked in my destination table. 我的目标表中还选中了“保留空值”选项。
  4. Here's the contents of the first text file 这是第一个文本文件的内容

      1002,"Murphy","Diane","x5800","dmurphy@classicmodelcars.com","1",NULL,"President" 1056,"Patterson","Mary","x4611","mpatterso@classicmodelcars.com","1",1002,"VP Sales" 1076,"Firrelli","Jeff","x9273","jfirrelli@classicmodelcars.com","1",1002,"VP Marketing" 1088,"Patterson","William","x4871","wpatterson@classicmodelcars.com","6",1056,"Sales Manager (JAPAN, APAC)" 1102,"Bondur","Gerard","x5408","athompson@classicmodelcars.com","4",1056,"Sale Manager (EMEA)" 

The first row has a null value in the second last column, and this first row is the cause of error. 第一行在最后第二列中具有空值,并且此第一行是错误原因。 Its the old error message, 它是旧的错误消息,

[Orders Text [70]] Error: Data conversion failed. The data conversion for column "ShippedDate" returned status value 2 and status text "The value could not be converted because of a potential loss of data.".

Similar thing happens with another text file where the Shippeddate has a null value. 对于另一个文本文件,其中Shippeddate具有null值,也会发生类似的情况。

I understand that since I am reading from a text file, it reads the NULL as text and not just NULL. 我了解,由于我正在从文本文件中读取内容,因此它将NULL读取为文本,而不仅仅是NULL。 So I can use the derived column and use the replace function in string to replace it with another value. 因此,我可以使用派生的列,并在字符串中使用replace函数将其替换为另一个值。

Also in the case of dates which look like this 同样在日期看起来像这样的情况下

           2003/1/13 0:00:00

I can't even replace them with a fictional date. 我什至不能用虚构的日期代替它们。 It still throws me an error after reading it as string, replacing using derived function and then converting using data conversion function. 在将其读取为字符串,使用派生函数进行替换,然后使用数据转换函数进行转换之后,仍然引发错误。

My question is "How to replace these values as NULL and not the string NULL in various datatypes in sql server"? 我的问题是“如何在SQL Server的各种数据类型中将这些值替换为NULL而不是字符串NULL”?

Also if possible, What do I do such that it reads NULL as NULL and not string. 另外,如果可能的话,我该怎么做,以使其将NULL读取为NULL而不是字符串。 I want it to be NULL and not replace it with some random value or 0. Its important that the missing data be shown as missing. 我希望它为NULL,而不是将其替换为某个随机值或0。重要的是,丢失的数据应显示为丢失。

Is far as I know NULL (empty) value from TXT source files (using Flat File Connection Manager) is only where no value is present. 据我所知,TXT源文件(使用平面文件连接管理器)中的NULL(空)值仅存在于没有值的地方。

So your record: 所以你的记录:

1002,"Murphy","Diane","x5800","dmurphy@classicmodelcars.com","1",NULL,"President"

... should be like this ...应该像这样

1002,"Murphy","Diane","x5800","dmurphy@classicmodelcars.com","1",,"President"

... to have NULL (empty) value. ...具有NULL(空)值。

I think the best way is to import everything as STRING, then REPLACE "NULL" values with NULL (empty) and then format everithing as needed in destination DB. 我认为最好的方法是将所有内容导入为STRING,然后用NULL(空)替换“ NULL”值,然后根据需要在目标数据库中进行格式化。

For NULL-ing your data you could use add new column on Derived Column transformation: 要对数据进行NULL处理,可以在派生列转换中使用添加新列:

ColX == "NULL" ? NULL(DT_WSTR, 50) : ColX

For dates also import as STRING, and with SUBSTRING (and FINDSTRING) expressions build the right format for converting into desired DATETIME format. 对于日期,还导入为STRING,并使用SUBSTRING(和FINDSTRING)表达式构建正确的格式,以转换为所需的DATETIME格式。

BR BR

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

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