I tried many times to pass the actual TAB character to SQLCMD and I just can't get it to take it. My favorite job is to pass SQLCMD ASCII "Unit Separator", which is the hexadecimal 0x1F, and can be entered on the command line by typing Ctrl-_ (the underscore control character, which on the US keyboard becomes ctrl-shift -'- '("- "next to" 0 "in the top row of the keyboard).
The advantage of using "Unit Separator" is that it is very unlikely to be present in the text of any description and was developed specifically for this purpose (see https://en.wikipedia.org/wiki/Delimiter )
Having received the SQLCMD for this, I then pass its output using a Unix-style translation command, like:
tr '\037' '\t'
\ 037 is the octal for "Unit Separator", and \ t represents the tab character, "tr" translates BOTH of them for us, we do not need to rely on any quote tricks in our scripts or shells.
To get 'tr' in windows, you can install the CoreUtils package from GnuWin32 (see http://gnuwin32.sourceforge.net/packages/coreutils.htm ) or go hard and install a full Unix environment like Cygwin (http : //cygwin.com/).
Putting two together, we get:
sqlcmd ... -h-1 -W -k -r1 -s^_ ... | tr '\037' '\t'
and this will give you tabbed results.
Look at the other parameters that I used above, they are necessary to get pure output from SQLCMD (in order, without headers, trimming spaces, CRLF for spaces, errors in STDERR (not your output file!), And "^ _" is how Unit Separator appears on the command line). You also need to add "SET NOCOUNT ON"; to your query or sql script, otherwise you will get a line counter as a test message appearing on your output!
dsz
source share