Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting bcp.exe to escape terminators

I need to export some data using SQL Server 2000's BCP utility. Sometimes my data contains characters, such as \t and \n, that I need to use as column and row terminators. How do I get BCP to escape characters it's using as terminators as it outputs the data, so that I can actually import the data in another program?

For example, one of my columns is text data, and includes tabs and newlines. BCP just exports them as-is, and the program I'm trying to import them with gets confused because the data ends in the middle of a line and/or a line contains extra columns for no apparent reason.

This seems like a very, very, very basic function to include in a data exporter, but none of the command-line options seem to mention it. (Why it wouldn't just be the default is beyond me.) Am I missing something?

like image 633
Kev Avatar asked Dec 29 '09 18:12

Kev


1 Answers

You can use a separator made up of multiple characters if you put them between double quotes:

bcp MY_TABLE out C:\MY_FILE.txt -S SERVER_IP -d DB_NAME -U MY_USER -P MY_PASSWORD -w -t "&#)^@" -r ">~+!"

Found the solution here.

like image 148
Cosmin Avatar answered Oct 25 '22 03:10

Cosmin