dawntrader
dawntrader

Reputation: 785

How to use TAB as column separator in SQLCMD

SQLCMD supports the -s parameter to specify the column separator, but I couldn't figure how how to represent the tab (CHAR(9)) character. I have tried the following but both don't work:

sqlcmd -S ServerName -E -Q"select * from mytable" -s"\t" -o results.txt
sqlcmd -S ServerName -E -Q"select * from mytable" -s'\t' -o results.txt

Any ideas how to do this in SQLCMD?

Upvotes: 14

Views: 56630

Answers (13)

Punit
Punit

Reputation: 39

sqlcmd -S ServerName -E -Q"select * from mytable" -s $'\t' -W -o results.txt

Upvotes: 0

Maurice1408
Maurice1408

Reputation: 319

If on Linux then this will work as the -s (col_separator)

-s "$(printf "t" | tr 't' '\t')" or

SQLCMDCOLSEP="$(printf "t" | tr 't' '\t')" sqlcmd -S ...

Upvotes: -1

EzPaulZ
EzPaulZ

Reputation: 313

I had this problem while trying to run sqlcmd on terminal. I got it working by entering a tab character (copying from text editor didn't work for me).

Press cntrl + v then tab.

How to enter a tab char on command line?

Upvotes: 0

David Morrow
David Morrow

Reputation: 294

Try using horizontal scroll bars with cmd.exe or powershell. Right click shortcut and click properties for repeated use, or right click title bar and click properties after opening then click layout tab. In screen buffer size set width and height to 8000 and then unselect wrap text output on resize (important). Click ok. Then restore down by clicking button next to minimize. You should see horizontal and vertical scroll bars. You can maximize window now and scroll in any direction. Now you can see all records in database.

Upvotes: 0

user3736075
user3736075

Reputation: 9

Use dynamic sql with CHAR(9):

SET @cmd ='SQLCMD -S MyServer -d MyDatabase -E -W -Q "SELECT * FROM MyTable" -s"' + CHAR(9) + '" -o "MyFilePath.txt"'

Upvotes: 0

Carlos Flores
Carlos Flores

Reputation: 11

To work in the Command Prompt window instead in batch file, this is the only way that I have found to solve it:

sqlcmd -S ServerName -E -d database_Name -Q"select col1, char(9), col2, char(9), col3, char(9), col4, char(9), col5 from mytable" -o results.txt -W -w 1024 -s "" -m 1

Upvotes: 1

Albert
Albert

Reputation: 396

tldr: use ALT+009 the ascii tab code for the separator character

In the example, replace {ALTCHAR} with ALT+009 (hold the ALT key and enter the digits 009)

sqlcmd -E -d tempdb -W -s "{ALTCHAR}" -o junk.txt -Q "select 1 c1,2 c2,3 c3"

Edit junk.txt. Tabs will be between columns.

For other command line options:

sqlcmd -?

Note: The shell converts the ALT char to ^I, but if you try the command by typing -s "^I", you won't get the same results.

Upvotes: 0

Goldbug
Goldbug

Reputation: 605

To achieve this using sqlcmd you need to use the Tab character like so: \t An example query exporting a single sql database table into a text file using a tab delimiter is as follows:

sqlcmd -S ServerName -d databaseTableName -Q "SELECT * FROM TABLE_NAME" -o C:\backups\tab_delimiter_bakup.txt -s"\t"

Upvotes: -1

David C.
David C.

Reputation: 405

A similar answer to one posted above, but it's simpler in a way that I think is significant.

  1. Open your text editor
  2. Press Tab
  3. Highlight the chunk of whitespace (the tab) created
  4. Copy and paste that into the spot in your SQL command

Even though this tab is represented as a wide chunk of whitespace, it is a single character.

The other answer had some unnecessary stuff about pasting the whole command with "<TAB>" in it. I think that throws people off (it certainly threw me off).

Upvotes: 3

JWally
JWally

Reputation: 592

Found a good answer here: SQLCMD outfile as tab delimited text file

  1. Open Notepad
  2. Paste this: sqlcmd -S (local) -E -s"<TAB>" -Q "select * from sys.dm_exec_query_stats" -o MyOutput.txt -h-1 -W
  3. Highlight <TAB>, then hit the Tab key
  4. Save the file as MyBatch.bat
  5. Run MyBatch.bat

Upvotes: 7

dsz
dsz

Reputation: 5222

I've tried numerous times to pass the actual TAB character in to SQLCMD, and I simply can't get it to take it. My favorite work-around to-date is to pass SQLCMD the ASCII "Unit Separator", which is hex 0x1F, and can be entered on the command line by typing Ctrl-_ (control underscore, which on a US keyboard becomes ctrl-shift-'-' (the '-' next to the '0' on the top row of the keyboard).

The advantage of using the 'Unit Separator' is that is is HIGHLY unlikely to be present in text of any description, and was designed specifically for this purpose (see https://en.wikipedia.org/wiki/Delimiter)

Having got SQLCMD to do that for me, I then pipe it's output though a Unix-style translate command as:

tr '\037' '\t'

The \037 is octal for the 'Unit Separator', and \t represents the tab character, 'tr' will translate BOTH of these for us, we don't need to rely on any quoting tricks in our scripts or shells.

To get 'tr' on windows, you can install the CoreUtils package from GnuWin32 (see http://gnuwin32.sourceforge.net/packages/coreutils.htm) or go heavy-weight and install a full Unix environment such as Cygwin (http://cygwin.com/).

Putting the two together we get:

sqlcmd ... -h-1 -W -k -r1 -s^_ ... | tr '\037' '\t'

and this will give you your output with tabs.

Look up the other options I've used above, they're essential for trying to get clean output from SQLCMD (in order; no headers, trim white-space, CRLF to spaces, errors to STDERR (not your output file!) and the '^_' is how the Unit Separator will appear on the command line). You'll also need to add "SET NOCOUNT ON;" to your query or sql script, otherwise you'll get the row-count as a trialling message appearing in your output!

Upvotes: 4

Doogie
Doogie

Reputation: 815

In a batch file, putting a tab between the double quotes works.

sqlcmd -S ServerName -E -Q"select * from mytable" -s"   " -o results.txt

to do the same in a PowerShell file use escaped double quotes wrapped around an escaped tab

sqlcmd -S ServerName -E -Q"select * from mytable" -s `"`t`" -o results.txt

Upvotes: 27

Ed Harper
Ed Harper

Reputation: 21505

It's difficult to get unformatted results from SQLCMD.

If you want to create a tab-delimited output file, BCP might be a better bet:

bcp "select * from mytable" queryout results.txt -S server -T -c

Upvotes: 10

Related Questions