23

I tried

UPDATE TABLENAME SET COLUMNNAME = REPLACE(COLUMNNAME, '\t', '')

But I don't know how to write the TAB in t-sql

13

In the beginning of my TSql sProcs, I often put

   Declare @nl Char(2) = char(13) + char(10)
   Declare @tab Char(1) = char(9)
   etc...

Then you can use those declared variables anywhere in the rest of the proc without loss of clarity...

  • Thanks for this, I separated out the CRLF characters and added some more that we were having issues with when casting a varchar to xml: Declare @cr Char(1) = char(13) Declare @lf Char(1) = char(10) Declare @tab Char(1) = char(9) Declare @pound Char(1) = char(163) Declare @uScore Char(1) = char(150) I gave up trying to make this comment pretty, the Markdown Editing guidelines say double space for a new line, but that didn't work! – GarethPN Sep 29 '16 at 15:20
23

For TAB and ENTER

SELECT
    -- TRIM
    LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(columnname, CHAR(9), ' '), CHAR(13), ' '), CHAR(10), ' ')))
3

I found the solution:

In T-SQL you do not escape characters, you paste or type them directly into the quotes. It works even for \r\n (carriage return, new line = you press enter)

  • 16
    I would advise caution with this. Six months down the line, are you going to remember what all of those blank spaces wrapped in single-quotes are for? – Adrien Aug 7 '09 at 16:24
4

You can put a tab character in the string, just press the tab key.

That will work, but it's not very readable.

43

The ASCII code for tab is 9; you could try

update tablename set columnname = replace(columnname, char(9), '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.