How to make Microsoft BCP export empty string instead of a NUL char?

雨燕双飞 提交于 2019-11-27 17:42:22

问题


I was working trying to get this bcp tool to work in a particular way. The -c switch is supposed to export using chars, but for some reason there was a weird char showing in Notepad++ like if it was UNICODE or some other formatting.

I wanted to get that char, which was an Empty string in the database, to export as an empty string into the text file. How do you do that?


回答1:


Well, after looking around the web this is what I found. Thought to put it in SO so more people have access to it.

http://www.techtalkz.com/microsoft-sql-server/147106-how-export-empty-strings-via-bcp.html

But when I run bcp, the empty strings are written to the text file using the ASCII "NUL" character (0x0). Instead, I want the field to be filled with blanks (0x20).

Solution:

In a text file, an empty string is interpreted as NULL when you bulk in data. The NUL character is interpreted as the empty string, so there is consistency. But that does not help much if you are exporting data to another system....

Simply wrap around a NULLIF that field that is possibly empty in the database and make it catch '' and replace with NULL. That will get converted into ^^values^^^^more values^^and more^^ so you get double separator with an blank instead of the NUL character.

E.g.:

...
NULLIF(pri.InstanceName, '') as PerformanceInstanceName,
...

Hope this helps someone.




回答2:


Just a follow-up...

The NULLIF solution above works fine in Microsoft SQL, but for a more standardized solution that works in almost any Database you should try COALESCE.

The syntax is very similar:

COALESCE(pri.InstanceName, '') as PerformanceInstanceName


来源:https://stackoverflow.com/questions/12588149/how-to-make-microsoft-bcp-export-empty-string-instead-of-a-nul-char

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