Inserting TAB into string

  • Hi all,

    This is for SQL 2000. I am trying to add a tab character to a string using char(9) but all I get is a single space.

    Example:

    select 'there should be a tab between here' + char(9) + 'and here'

    Ideas?

    Thanks.

  • What are you using to verify that you only get a space? You're not going to see it in QA or SSMS, as they don't show things like carriage returns/tabs/etc. If you select that cell into a front end that does display those things, it may work fine.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Ahh, I was only looking in QA then copy/paste to Notepad. The finished output does have the tab.

    Thanks.

  • RD-201664 (10/6/2009)


    select 'there should be a tab between here' + char(9) + 'and here'

    Ideas?

    declare @result varchar(100)

    set @result = 'TEST START'+char(9)+'EXEC'+char(9)+'TEST END'

    select @result

    RESULT

    TEST STARTEXECTEST END

    set @result = replace(@result,char(9),' -TAB HERE- ')

    select @result

    RESULT

    TEST START -TAB HERE- EXEC -TAB HERE- TEST END

  • [font="Arial"]I add the same problem with SQL Server :

    "something"+char(9)+"something"

    returns something something

    "something"+char(9) + char(9)+char(9)+char(9)+char(9)+char(9)+"something"

    returns something something

    finally, I "cheated" with :

    "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)

    so visually I got what I wanted, even if I admit it is not very elegant...[/font]

  • I solved this by just creating a blank tab column, then copying both columns.  It pastes perfectly with a tab.

    SELECT '' AS Tab, 'Text following tab.' AS OtherText

    I use this method when I want to generate well-tabbed SQL code.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply