Amit Singh Parihar
Amit Singh Parihar

Reputation: 557

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.

  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","[email protected]","1",NULL,"President"
     1056,"Patterson","Mary","x4611","[email protected]","1",1002,"VP Sales"
     1076,"Firrelli","Jeff","x9273","[email protected]","1",1002,"VP Marketing"
     1088,"Patterson","William","x4871","[email protected]","6",1056,"Sales Manager (JAPAN, APAC)"
     1102,"Bondur","Gerard","x5408","[email protected]","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.

I understand that since I am reading from a text file, it reads the NULL as text and not just NULL. So I can use the derived column and use the replace function in string to replace it with another value.

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"?

Also if possible, What do I do such that it reads NULL as NULL and not string. 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.

Upvotes: 2

Views: 2803

Answers (1)

Žiga Prajs
Žiga Prajs

Reputation: 211

Is far as I know NULL (empty) value from TXT source files (using Flat File Connection Manager) is only where no value is present.

So your record:

1002,"Murphy","Diane","x5800","[email protected]","1",NULL,"President"

... should be like this

1002,"Murphy","Diane","x5800","[email protected]","1",,"President"

... to have NULL (empty) value.

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.

For NULL-ing your data you could use add new column on Derived Column transformation:

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.

BR

Upvotes: 3

Related Questions