shrekDeep
shrekDeep

Reputation: 2328

Char(9) doesn't print tab in Sql server 2005

I am trying to print a tab in sql server using:

select 'tab-->' + char(9) + '<--tab'

But it doesn't seem to work and always prints

tab--> <--tab

Is there any thing I am missing?

Upvotes: 7

Views: 21052

Answers (4)

Dzmitry Paliakou
Dzmitry Paliakou

Reputation: 1627

  1. Tool -> Options -> Query Results -> SQL Server -> Results to Grid -> Retain CR/LF on copy or save
  2. Restart SSMS

Upvotes: 1

Soucatra
Soucatra

Reputation: 1

Each Char(9) or char(160) is converted to a space. If you align char(9)+char(9)+char(9) => you still got only 1 space.

So I tried these :

<"something"+char(9)+char(160)+char(9)+char(160)+char(9)+char(160)+char(9)+char(160)"something">

returns something[8 spaces here]something (each char9 or char16 is replaced by a space)

Visually, I got what I wanted...

Upvotes: 0

Ben Thul
Ben Thul

Reputation: 32707

The thing about tabs is that they aren't a constant width. A tab character says "bring me to the next tab stop". If you currently have tab stops set at eight characters and the tab is at character six, the tab will only be one wide. But let's say you're skeptical (a good thing!). There's more evidence that the tab is actually being output. If you do something like print '>' + char(9) + '<', you'll notice that you can't select only one space in the gap, indicating that the whitespace that you're seeing is atomic. But more convincing to me is to paste it into an editor that allows you to see tabs. I use vim, but use whatever you like. In all cases I tested, the tab character showed up.

Upvotes: 0

Michael Fredrickson
Michael Fredrickson

Reputation: 37388

If you're testing this inside of Management Studio, the Results to Grid (Ctrl + D) setting will change your tab to a space... try switching to Results to Text (Ctrl + T) instead, and you will see the tab.

Alternately, you can change your select to a print:

print 'tab-->' + char(9) + '<--tab'

Outputs...

tab-->  <--tab

Upvotes: 18

Related Questions