如何删除t-sql中的不可见字符?

时间:2009-08-07 16:13:26

标签: tsql

我试过

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

但我不知道如何在t-sql中编写TAB

7 个答案:

答案 0 :(得分:43)

标签的ASCII代码是9;你可以尝试

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

答案 1 :(得分:23)

适用于TABENTER

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

答案 2 :(得分:12)

在我的TSql sProcs的开头,我经常把

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

然后你可以在proc的其余部分的任何地方使用那些声明的变量,而不会失去清晰度......

答案 3 :(得分:4)

您可以在字符串中添加制表符,只需按Tab键即可。

这样可行,但不太可读。

答案 4 :(得分:3)

我找到了解决方案:

在T-SQL中,您不会转义字符,而是将它们直接粘贴或键入引号。它甚至适用于\ r \ n(回车,新行=你按回车)

答案 5 :(得分:2)

我在上面尝试过的评论之一只是读取数据,而没有实际更新数据,我在以下方面取得了最大的成功

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

我知道这是一个古老的问题,希望对别人有帮助

答案 6 :(得分:1)

  

签出此功能。这将删除所有无效字符

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