开发者

SQL Server Bulk Insert from Fixed Width File- How do I get NULLs for strings?

开发者 https://www.devze.com 2023-01-11 17:35 出处:网络
I\'m doing a bulk insert from a fixed width text file using INSERT INTO blah SELECT blah1, blah2 FROM OPENROWSET(BULK \'filename.txt\', FORMATFILE=\'format.xml\');

I'm doing a bulk insert from a fixed width text file using

INSERT INTO blah
SELECT blah1, blah2 
  FROM OPENROWSET(BULK 'filename.txt', FORMATFILE='format.xml');

It's working fine, except that I want NULLs for the empty (all spaces) fields in the file. This is no problem for fields marked in the format file as SQLINT, SQLDATETIME, etc., but SQ开发者_StackOverflow中文版LNVARCHAR types just come in as empty strings.

I suppose that does make sense, but how would I get it to import NULLs instead?


I always import to a staging table and do the necessary clean up and then insert to production tables.


Use a CASE statement

SELECT CASE
           WHEN RTRIM(blah2) = '' THEN NULL
                                  ELSE blah2
       END
0

精彩评论

暂无评论...
验证码 换一张
取 消