Why does bcp output null when the column contains an empty string and empty string when the column is null?

≯℡__Kan透↙ 提交于 2019-12-04 11:04:42

问题


This struck me as really weird behaviour and I spent a while checking for bugs in my code before I found this

"out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string." (from http://msdn.microsoft.com/en-us/library/ms162802.aspx)

Obviously this allowed me to fix my problem but can anybody think of or does anybody know a reason why this is the case?


回答1:


It's been some time, but I'm sure it's a backwards compatibility/legacy back to SQL Server 6.5

SQL Server 6.5 could not store empty string: there was always one space. This changed with SQL 7

So '' -> NULL and ' ' -> '' is correct from an ancient history perspective.




回答2:


SELECT ARTICULO as Articulo, 
        case when rtrim(CCOLOR) = '' then null else rtrim(CCOLOR) end as Color, 
        case when rtrim(TALLE) = '' then null else rtrim(TALLE) end as Talle,
from precios

Send null in place of empty.

I find the best solution here:

https://bytes.com/topic/sql-server/answers/143738-bcp-inserting-blank-space-empty-string

I found a work around, using a Case structure in the sql query to change empty string to a null. The BCP in turn outputs the resulting null as an empty! Thanks for your assistance. Eric




回答3:


This is related to the "default values" section for BCP:

https://docs.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server

For example, if there is a null field in a data file, the default value for the column is loaded instead.

You have to think back to days where importing plain text files from other weird systems. BCP translates '' as "not defined"(=missing data) and sets a NULL in the database (=missing data). The other way around a NULL from database must be a '' for the other systems.

To get the 'real' data out of the database use the -k switch:

https://docs.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server#keep_nulls

The following qualifiers specify that an empty field in the data file retains its null value during the bulk-import operation, rather than inheriting a default value (if any) for the table columns.

Then you have your ASCII 0x0 in your file/database.



来源:https://stackoverflow.com/questions/1644731/why-does-bcp-output-null-when-the-column-contains-an-empty-string-and-empty-stri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!