Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...