30

I tried

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

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

7 Answers 7

47

The ASCII code for tab is 9; you could try

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

For TAB and ENTER

SELECT
    -- TRIM AND REPLACE `TAB` AND `ENTER`
    LTRIM(RTRIM(
        REPLACE(
            REPLACE(
                REPLACE(columnname, CHAR(9), ' '),
            CHAR(13), ' '),
        CHAR(10), ' ')
    ))
15

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...

1
  • 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
    Commented Sep 29, 2016 at 15:20
4

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

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

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)

1
  • 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
    Commented Aug 7, 2009 at 16:24
3

Checkout this function. This will remove every invalid char

-- =============================================
-- Author:      xenoivan
-- Description: clean invalid chars
-- =============================================
CREATE FUNCTION [dbo].[fnClean] 
(
    @in NVARCHAR(MAX)
)
RETURNS  NVARCHAR(MAX)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @out NVARCHAR(MAX)

    -- Add the T-SQL statements to compute the return value here
    SELECT @out = REPLACE(@in,N'َ','')
    SELECT @out = REPLACE(@out,char(9),'')
    SELECT @out = REPLACE(@out,char(13),'')
    SELECT @out = REPLACE(@out,char(10),'')
    SELECT @out = REPLACE(@out,N'‬','')
    SELECT @out = REPLACE(@out,N'‬','')
    SELECT @out = REPLACE(@out,N'‬‬','')
    SELECT @out = REPLACE(@out,N'‎', '')--its a hidden character
    SELECT @out = REPLACE(@out,N'‎', '')--ltr code
    SELECT @out = REPLACE(@out,N'‎', '')--rtl code
    SELECT @out = REPLACE(@out,N'۰', '0')
    SELECT @out = REPLACE(@out,N'۱', '1')
    SELECT @out = REPLACE(@out,N'۲', '2')
    SELECT @out = REPLACE(@out,N'۳', '3')
    SELECT @out = REPLACE(@out,N'۴', '4')
    SELECT @out = REPLACE(@out,N'۵', '5')
    SELECT @out = REPLACE(@out,N'۶', '6')
    SELECT @out = REPLACE(@out,N'۷', '7')
    SELECT @out = REPLACE(@out,N'۸', '8')
    SELECT @out = REPLACE(@out,N'۹', '9')
    SELECT @out = REPLACE(@out,N'٠', '0')
    SELECT @out = REPLACE(@out,N'١', '1')
    SELECT @out = REPLACE(@out,N'٢', '2')
    SELECT @out = REPLACE(@out,N'٣', '3')
    SELECT @out = REPLACE(@out,N'٤', '4')
    SELECT @out = REPLACE(@out,N'٥', '5')
    SELECT @out = REPLACE(@out,N'٦', '6')
    SELECT @out = REPLACE(@out,N'٧', '7')
    SELECT @out = REPLACE(@out,N'٨', '8')
    SELECT @out = REPLACE(@out,N'٩', '9');

    -- Return the result of the function
    RETURN @out

END
1
  • 2
    I would recomend using NCHAR() function. So instead of '' --ltr code you would use NCHAR(8206) --ltr code, or NCHAR(0x200E) --ltr code, which is more verbose and would survive even if you accidentally saved your file in a wrong encoding.
    – andowero
    Commented Jun 4, 2021 at 8:15
2

One of the comments I tried above was only reading the data and not actually updating the data, I had the best success with the following

UPDATE Tbl
SET Tbl.[ColumnName] = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(Tbl.[ColumnName], CHAR(9), ' '),CHAR(13), ' '),CHAR(10), ' ')))
FROM [TableName] AS Tbl

I know this is a old question hope this helps someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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