How to remove invisible characters in t-sql?

后端 未结 7 920
天命终不由人
天命终不由人 2021-02-02 09:07

I tried

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

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

相关标签:
7条回答
  • 2021-02-02 09:18

    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)

    0 讨论(0)
  • 2021-02-02 09:24

    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
    
    0 讨论(0)
  • 2021-02-02 09:27

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

    0 讨论(0)
  • 2021-02-02 09:32

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

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

    0 讨论(0)
  • 2021-02-02 09:36

    The ASCII code for tab is 9; you could try

    update tablename set columnname = replace(columnname, char(9), '')
    
    0 讨论(0)
  • 2021-02-02 09:43

    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

    0 讨论(0)
提交回复
热议问题