去除sql中不可见字符的n种方法

CREATE TABLE [ASCII0127]

(

Bin          INT,

Dec          INT,

Hex          VARCHAR(128),

Abbr         VARCHAR(128),

Description  NVARCHAR(256),

)

 

 

"INSERT INTO (Bin, Dec, Hex, Abbr, Description)

SELECT REPLACE(STR('"&&"',8,0),' ', '0'), '"&&"', REPLACE(STR('"&&"',2,0),' ', '0'), '"&&"', N'"&&"'

UNION ALL"

 

"SELECT REPLACE(STR('"&&"',8,0),' ', '0'), '"&&"', '"&&"', '"&&"', N'"&&"'

UNION ALL"

 

 

 

INSERT INTO [ASCII0127](Bin, Dec, Hex, Abbr, Description)

 

SELECT REPLACE(STR('0',8,0),' ', '0'), '0', REPLACE(STR('0',2,0),' ', '0'), 'NUL(null)', N'空字符'

UNION ALL

SELECT REPLACE(STR('1',8,0),' ', '0'), '1', REPLACE(STR('1',2,0),' ', '0'), 'SOH(start of headling)', N'标题开始'

UNION ALL

SELECT REPLACE(STR('10',8,0),' ', '0'), '2', REPLACE(STR('2',2,0),' ', '0'), 'STX (start of text)', N'正文开始'

UNION ALL

SELECT REPLACE(STR('11',8,0),' ', '0'), '3', REPLACE(STR('3',2,0),' ', '0'), 'ETX (end of text)', N'正文结束'

UNION ALL

SELECT REPLACE(STR('100',8,0),' ', '0'), '4', REPLACE(STR('4',2,0),' ', '0'), 'EOT (end of transmission)', N'传输结束'

UNION ALL

SELECT REPLACE(STR('101',8,0),' ', '0'), '5', REPLACE(STR('5',2,0),' ', '0'), 'ENQ (enquiry)', N'请求'

UNION ALL

SELECT REPLACE(STR('110',8,0),' ', '0'), '6', REPLACE(STR('6',2,0),' ', '0'), 'ACK (acknowledge)', N'收到通知'

UNION ALL

SELECT REPLACE(STR('111',8,0),' ', '0'), '7', REPLACE(STR('7',2,0),' ', '0'), 'BEL (bell)', N'响铃'

UNION ALL

SELECT REPLACE(STR('1000',8,0),' ', '0'), '8', REPLACE(STR('8',2,0),' ', '0'), 'BS (backspace)', N'退格'

UNION ALL

SELECT REPLACE(STR('1001',8,0),' ', '0'), '9', REPLACE(STR('9',2,0),' ', '0'), 'HT (horizontal tab)', N'水平制表符'

UNION ALL

 

SELECT REPLACE(STR('1010',8,0),' ', '0'), '10', '0A', 'LF (NL line feed, new line)', N'换行键'

UNION ALL

SELECT REPLACE(STR('1011',8,0),' ', '0'), '11', '0B', 'VT (vertical tab)', N'垂直制表符'

UNION ALL

SELECT REPLACE(STR('1100',8,0),' ', '0'), '12', '0C', 'FF (NP form feed, new page)', N'换页键'

UNION ALL

SELECT REPLACE(STR('1101',8,0),' ', '0'), '13', '0D', 'CR (carriage return)', N'回车键'

UNION ALL

SELECT REPLACE(STR('1110',8,0),' ', '0'), '14', '0E', 'SO (shift out)', N'不用切换'

UNION ALL

SELECT REPLACE(STR('1111',8,0),' ', '0'), '15', '0F', 'SI (shift in)', N'启用切换'

UNION ALL

SELECT REPLACE(STR('10000',8,0),' ', '0'), '16', '10', 'DLE (data link escape)', N'数据链路转义'

UNION ALL

SELECT REPLACE(STR('10001',8,0),' ', '0'), '17', '11', 'DC1 (device control 1)', N'设备控制'

UNION ALL

SELECT REPLACE(STR('10010',8,0),' ', '0'), '18', '12', 'DC2 (device control 2)', N'设备控制'

UNION ALL

SELECT REPLACE(STR('10011',8,0),' ', '0'), '19', '13', 'DC3 (device control 3)', N'设备控制'

UNION ALL

SELECT REPLACE(STR('10100',8,0),' ', '0'), '20', '14', 'DC4 (device control 4)', N'设备控制'

UNION ALL

SELECT REPLACE(STR('10101',8,0),' ', '0'), '21', '15', 'NAK (negative acknowledge)', N'拒绝接收'

UNION ALL

SELECT REPLACE(STR('10110',8,0),' ', '0'), '22', '16', 'SYN (synchronous idle)', N'同步空闲'

UNION ALL

SELECT REPLACE(STR('10111',8,0),' ', '0'), '23', '17', 'ETB (end of trans. block)', N'传输块结束'

UNION ALL

SELECT REPLACE(STR('11000',8,0),' ', '0'), '24', '18', 'CAN (cancel)', N'取消'

UNION ALL

SELECT REPLACE(STR('11001',8,0),' ', '0'), '25', '19', 'EM (end of medium)', N'介质中断'

UNION ALL

SELECT REPLACE(STR('11010',8,0),' ', '0'), '26', '1A', 'SUB (substitute)', N'替补'

UNION ALL

SELECT REPLACE(STR('11011',8,0),' ', '0'), '27', '1B', 'ESC (escape)', N'溢出'

UNION ALL

SELECT REPLACE(STR('11100',8,0),' ', '0'), '28', '1C', 'FS (file separator)', N'文件分割符'

UNION ALL

SELECT REPLACE(STR('11101',8,0),' ', '0'), '29', '1D', 'GS (group separator)', N'分组符'

UNION ALL

SELECT REPLACE(STR('11110',8,0),' ', '0'), '30', '1E', 'RS (record separator)', N'记录分离符'

UNION ALL

SELECT REPLACE(STR('11111',8,0),' ', '0'), '31', '1F', 'US (unit separator)', N'单元分隔符'

UNION ALL

SELECT REPLACE(STR('100000',8,0),' ', '0'), '32', '20', '(space)', N'空格'

UNION ALL

SELECT REPLACE(STR('100001',8,0),' ', '0'), '33', '21', '!', N''

UNION ALL

SELECT REPLACE(STR('100010',8,0),' ', '0'), '34', '22', '', N''

UNION ALL

SELECT REPLACE(STR('100011',8,0),' ', '0'), '35', '23', '#', N''

UNION ALL

SELECT REPLACE(STR('100100',8,0),' ', '0'), '36', '24', '$', N''

UNION ALL

SELECT REPLACE(STR('100101',8,0),' ', '0'), '37', '25', '%', N''

UNION ALL

SELECT REPLACE(STR('100110',8,0),' ', '0'), '38', '26', '&', N''

UNION ALL

SELECT REPLACE(STR('100111',8,0),' ', '0'), '39', '27', '''', N''

UNION ALL

SELECT REPLACE(STR('101000',8,0),' ', '0'), '40', '28', '(', N''

UNION ALL

SELECT REPLACE(STR('101001',8,0),' ', '0'), '41', '29', ')', N''

UNION ALL

SELECT REPLACE(STR('101010',8,0),' ', '0'), '42', '2A', '*', N''

UNION ALL

SELECT REPLACE(STR('101011',8,0),' ', '0'), '43', '2B', '+', N''

UNION ALL

SELECT REPLACE(STR('101100',8,0),' ', '0'), '44', '2C', ',', N''

UNION ALL

SELECT REPLACE(STR('101101',8,0),' ', '0'), '45', '2D', '-', N''

UNION ALL

SELECT REPLACE(STR('101110',8,0),' ', '0'), '46', '2E', '.', N''

UNION ALL

SELECT REPLACE(STR('101111',8,0),' ', '0'), '47', '2F', '/', N''

UNION ALL

SELECT REPLACE(STR('110000',8,0),' ', '0'), '48', '30', '0', N''

UNION ALL

SELECT REPLACE(STR('110001',8,0),' ', '0'), '49', '31', '1', N''

UNION ALL

SELECT REPLACE(STR('110010',8,0),' ', '0'), '50', '32', '2', N''

UNION ALL

SELECT REPLACE(STR('110011',8,0),' ', '0'), '51', '33', '3', N''

UNION ALL

SELECT REPLACE(STR('110100',8,0),' ', '0'), '52', '34', '4', N''

UNION ALL

SELECT REPLACE(STR('110101',8,0),' ', '0'), '53', '35', '5', N''

UNION ALL

SELECT REPLACE(STR('110110',8,0),' ', '0'), '54', '36', '6', N''

UNION ALL

SELECT REPLACE(STR('110111',8,0),' ', '0'), '55', '37', '7', N''

UNION ALL

SELECT REPLACE(STR('111000',8,0),' ', '0'), '56', '38', '8', N''

UNION ALL

SELECT REPLACE(STR('111001',8,0),' ', '0'), '57', '39', '9', N''

UNION ALL

SELECT REPLACE(STR('111010',8,0),' ', '0'), '58', '3A', ':', N''

UNION ALL

SELECT REPLACE(STR('111011',8,0),' ', '0'), '59', '3B', ';', N''

UNION ALL

SELECT REPLACE(STR('111100',8,0),' ', '0'), '60', '3C', '<', N''

UNION ALL

SELECT REPLACE(STR('111101',8,0),' ', '0'), '61', '3D', '=', N''

UNION ALL

SELECT REPLACE(STR('111110',8,0),' ', '0'), '62', '3E', '>', N''

UNION ALL

SELECT REPLACE(STR('111111',8,0),' ', '0'), '63', '3F', '?', N''

UNION ALL

SELECT REPLACE(STR('1000000',8,0),' ', '0'), '64', '40', '@', N''

UNION ALL

SELECT REPLACE(STR('1000001',8,0),' ', '0'), '65', '41', 'A', N''

UNION ALL

SELECT REPLACE(STR('1000010',8,0),' ', '0'), '66', '42', 'B', N''

UNION ALL

SELECT REPLACE(STR('1000011',8,0),' ', '0'), '67', '43', 'C', N''

UNION ALL

SELECT REPLACE(STR('1000100',8,0),' ', '0'), '68', '44', 'D', N''

UNION ALL

SELECT REPLACE(STR('1000101',8,0),' ', '0'), '69', '45', 'E', N''

UNION ALL

SELECT REPLACE(STR('1000110',8,0),' ', '0'), '70', '46', 'F', N''

UNION ALL

SELECT REPLACE(STR('1000111',8,0),' ', '0'), '71', '47', 'G', N''

UNION ALL

SELECT REPLACE(STR('1001000',8,0),' ', '0'), '72', '48', 'H', N''

UNION ALL

SELECT REPLACE(STR('1001001',8,0),' ', '0'), '73', '49', 'I', N''

UNION ALL

SELECT REPLACE(STR('1001010',8,0),' ', '0'), '74', '4A', 'J', N''

UNION ALL

SELECT REPLACE(STR('1001011',8,0),' ', '0'), '75', '4B', 'K', N''

UNION ALL

SELECT REPLACE(STR('1001100',8,0),' ', '0'), '76', '4C', 'L', N''

UNION ALL

SELECT REPLACE(STR('1001101',8,0),' ', '0'), '77', '4D', 'M', N''

UNION ALL

SELECT REPLACE(STR('1001110',8,0),' ', '0'), '78', '4E', 'N', N''

UNION ALL

SELECT REPLACE(STR('1001111',8,0),' ', '0'), '79', '4F', 'O', N''

UNION ALL

SELECT REPLACE(STR('1010000',8,0),' ', '0'), '80', '50', 'P', N''

UNION ALL

SELECT REPLACE(STR('1010001',8,0),' ', '0'), '81', '51', 'Q', N''

UNION ALL

SELECT REPLACE(STR('1010010',8,0),' ', '0'), '82', '52', 'R', N''

UNION ALL

SELECT REPLACE(STR('1010011',8,0),' ', '0'), '83', '53', 'S', N''

UNION ALL

SELECT REPLACE(STR('1010100',8,0),' ', '0'), '84', '54', 'T', N''

UNION ALL

SELECT REPLACE(STR('1010101',8,0),' ', '0'), '85', '55', 'U', N''

UNION ALL

SELECT REPLACE(STR('1010110',8,0),' ', '0'), '86', '56', 'V', N''

UNION ALL

SELECT REPLACE(STR('1010111',8,0),' ', '0'), '87', '57', 'W', N''

UNION ALL

SELECT REPLACE(STR('1011000',8,0),' ', '0'), '88', '58', 'X', N''

UNION ALL

SELECT REPLACE(STR('1011001',8,0),' ', '0'), '89', '59', 'Y', N''

UNION ALL

SELECT REPLACE(STR('1011010',8,0),' ', '0'), '90', '5A', 'Z', N''

UNION ALL

SELECT REPLACE(STR('1011011',8,0),' ', '0'), '91', '5B', '[', N''

UNION ALL

SELECT REPLACE(STR('1011100',8,0),' ', '0'), '92', '5C', '\', N''

UNION ALL

SELECT REPLACE(STR('1011101',8,0),' ', '0'), '93', '5D', ']', N''

UNION ALL

SELECT REPLACE(STR('1011110',8,0),' ', '0'), '94', '5E', '^', N''

UNION ALL

SELECT REPLACE(STR('1011111',8,0),' ', '0'), '95', '5F', '_', N''

UNION ALL

SELECT REPLACE(STR('1100000',8,0),' ', '0'), '96', '60', '`', N''

UNION ALL

SELECT REPLACE(STR('1100001',8,0),' ', '0'), '97', '61', 'a', N''

UNION ALL

SELECT REPLACE(STR('1100010',8,0),' ', '0'), '98', '62', 'b', N''

UNION ALL

SELECT REPLACE(STR('1100011',8,0),' ', '0'), '99', '63', 'c', N''

UNION ALL

SELECT REPLACE(STR('1100100',8,0),' ', '0'), '100', '64', 'd', N''

UNION ALL

SELECT REPLACE(STR('1100101',8,0),' ', '0'), '101', '65', 'e', N''

UNION ALL

SELECT REPLACE(STR('1100110',8,0),' ', '0'), '102', '66', 'f', N''

UNION ALL

SELECT REPLACE(STR('1100111',8,0),' ', '0'), '103', '67', 'g', N''

UNION ALL

SELECT REPLACE(STR('1101000',8,0),' ', '0'), '104', '68', 'h', N''

UNION ALL

SELECT REPLACE(STR('1101001',8,0),' ', '0'), '105', '69', 'i', N''

UNION ALL

SELECT REPLACE(STR('1101010',8,0),' ', '0'), '106', '6A', 'j', N''

UNION ALL

SELECT REPLACE(STR('1101011',8,0),' ', '0'), '107', '6B', 'k', N''

UNION ALL

SELECT REPLACE(STR('1101100',8,0),' ', '0'), '108', '6C', 'l', N''

UNION ALL

SELECT REPLACE(STR('1101101',8,0),' ', '0'), '109', '6D', 'm', N''

UNION ALL

SELECT REPLACE(STR('1101110',8,0),' ', '0'), '110', '6E', 'n', N''

UNION ALL

SELECT REPLACE(STR('1101111',8,0),' ', '0'), '111', '6F', 'o', N''

UNION ALL

SELECT REPLACE(STR('1110000',8,0),' ', '0'), '112', '70', 'p', N''

UNION ALL

SELECT REPLACE(STR('1110001',8,0),' ', '0'), '113', '71', 'q', N''

UNION ALL

SELECT REPLACE(STR('1110010',8,0),' ', '0'), '114', '72', 'r', N''

UNION ALL

SELECT REPLACE(STR('1110011',8,0),' ', '0'), '115', '73', 's', N''

UNION ALL

SELECT REPLACE(STR('1110100',8,0),' ', '0'), '116', '74', 't', N''

UNION ALL

SELECT REPLACE(STR('1110101',8,0),' ', '0'), '117', '75', 'u', N''

UNION ALL

SELECT REPLACE(STR('1110110',8,0),' ', '0'), '118', '76', 'v', N''

UNION ALL

SELECT REPLACE(STR('1110111',8,0),' ', '0'), '119', '77', 'w', N''

UNION ALL

SELECT REPLACE(STR('1111000',8,0),' ', '0'), '120', '78', 'x', N''

UNION ALL

SELECT REPLACE(STR('1111001',8,0),' ', '0'), '121', '79', 'y', N''

UNION ALL

SELECT REPLACE(STR('1111010',8,0),' ', '0'), '122', '7A', 'z', N''

UNION ALL

SELECT REPLACE(STR('1111011',8,0),' ', '0'), '123', '7B', '{', N''

UNION ALL

SELECT REPLACE(STR('1111100',8,0),' ', '0'), '124', '7C', '|', N''

UNION ALL

SELECT REPLACE(STR('1111101',8,0),' ', '0'), '125', '7D', '}', N''

UNION ALL

SELECT REPLACE(STR('1111110',8,0),' ', '0'), '126', '7E', '~', N''

UNION ALL

SELECT REPLACE(STR('1111111',8,0),' ', '0'), '127', '7F', 'DEL (delete)', N'删除'

 

 

 

 

--/*

--去除特殊字符

--替換非數字,非英文,非漢字的

------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------

 

ALTER FUNCTION DBO.Invisible_Str

(@S NVARCHAR(MAX))

 

RETURNS NVARCHAR(MAX)

AS

BEGIN

 

DECLARE @i INT

    SET @i = PATINDEX('%[^-\./_@; ,^0-9,^a-z,^A-Z,^吖-座]%',CONVERT(VARCHAR(MAX),@S))

 

 

WHILE @i> 0

  Begin

    SET @S = STUFF(@S,  @i,  1, '')

    SET @i = PATINDEX('%[^-\./_@; ,^0-9,^a-z,^A-Z,^吖-座]%',CONVERT(VARCHAR(MAX),@S))

  End

    SET @S = RTRIM( LTRIM(@S) )

 

RETURN @S

END

 

 

 

 

 

--去除不可见字符

------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------

--方法:(转后去)

SELECT 'SELECT @Authentication=REPLACE(@Authentication, ''&#x'+Hex+';'' ,'''')' FROM ASCII0127 WHERE ([Dec] BETWEEN 0 AND 31) OR [Dec]=127

 

 

 

 

 

 

--方法:(去源)

DECLARE @TableName VARCHAR(MAX)='eEmployee' -->此处填写表名

DECLARE @ColName VARCHAR(MAX)='Badge' -->此处填写字段名

       

DECLARE @ReplaceCmd VARCHAR(MAX)=@ColName -->此处填写字段名

SELECT  @ReplaceCmd ='REPLACE('+ @ReplaceCmd+', CHAR('+CONVERT(VARCHAR(128),[Dec])+') ,'''')' FROM [ASCII0127] WHERE ([Dec] BETWEEN 0 AND 31) OR [Dec]=127

PRINT @ReplaceCmd

 

DECLARE @SQqlCmd NVARCHAR(MAX)=''

--SET @SQqlCmd = 'SELECT '+@ReplaceCmd+'AS NeCol FROM '+@TableName

SET @SQqlCmd = 'UPDATE '+@TableName+' SET '+@ColName+'='+@ReplaceCmd

SET @SQqlCmd = 'UPDATE '+@TableName+' SET '+@ColName+'='+RTRIM( LTRIM(@ColName) )

PRINT @SQqlCmd

 

EXEC (@SQqlCmd)

 

 

 

--方法-a:(推荐)

ALTER FUNCTION dbo.fun_getVisableStr

(@str NVARCHAR(MAX)) 

RETURNS NVARCHAR(MAX) 

AS

BEGIN

 

   declare @word nchar(1),

           @VisableStr nvarchar(4000) 

       set @VisableStr=''

 

 WHILE LEN(@str)>0 

 BEGIN

 

   IF UNICODE( LEFT(@str,1) ) NOT BETWEEN 0 AND 31 AND UNICODE( LEFT(@str,1) )<>127

   SET @VisableStr=@VisableStr + LEFT(@str,1)

  

   SET @str=right(@str,LEN(@str)-1) 

  

 END

   SET @VisableStr= RTRIM( LTRIM(@VisableStr) )

 

RETURN @VisableStr

 

END

 

--方法-b:SP

CREATE PROCEDURE SP_Invisible_Str ----------SP_Invisible_Str 'eEmployee'

@TblName NVARCHAR(258),

@RetVal INT=0 OUTPUT

 

AS

BEGIN

 

 

 

 

 

 

--事务(嵌套)控制

  Begin Try--开始捕捉

SET XACT_ABORT OFF

BEGIN TRANSACTION -->开始事务

-----------------------------------------------------------------------------------------------------------------------------------------------<

 

DECLARE @sql NVARCHAR(MAX)

    SET @sql = 'SET NOCOUNT ON' +CHAR(13)+ 'SET XACT_ABORT OFF'+CHAR(13)+CHAR(13)+'BEGIN'+CHAR(13)

   

 SELECT @sql =@sql+ '

 UPDATE '+@TblName+' SET '+col.name+'='+'dbo.fun_getVisableStr('+col.name+');'

  FROM syscolumns AS col INNER JOIN sysobjects  AS obj  ON col.ID = obj.ID INNER JOIN systypes  AS typ ON col.xtype = typ.xtype

 WHERE obj.xtype ='U'

   AND obj.name = @TblName

   AND typ.name IN ('VARCHAR','CHAR','NVARCHAR','NCHAR')

 

  SELECT @sql =@sql+

'

 

END

 

SET NOCOUNT OFF

SET XACT_ABORT ON'

--PRINT(@sql)

--SELECT @sql

EXEC SP_EXECUTESQL @sql

  

-----------------------------------------------------------------------------------------------------------------------------------------------<

COMMIT TRANSACTION -->提交事务

SET XACT_ABORT ON

  End Try

 

  Begin Catch

    Select Error_number() as ErrorNumber,  --错误代码

           Error_severity() as ErrorSeverity,  --错误严重级别,级别小于try catch 捕获不到

           Error_state() as ErrorState ,  --错误状态码

           Error_Procedure() as ErrorProcedure , --出现错误的存储过程或触发器的名称。

           Error_line() as ErrorLine,  --发生错误的行号

           Error_message() as ErrorMessage  --错误的具体信息

   

    IF(@@trancount>1) --全局变量@@trancount,事务开启此值+1,他用来判断是有开启事务

      COMMIT TRANSACTION -->回滚事务

    ELSE

      ROLLBACK TRANSACTION

     

    DECLARE @ErrorMessage NVARCHAR(4000)=ERROR_MESSAGE(),

            @ErrorSeverity INT= ERROR_SEVERITY(),

            @ErrorState INT=ERROR_STATE()

 -------------------------------------  

 

--/* 

IF @ErrorMessage IN (SELECT Title FROM skyMSGAlarm)

BEGIN

    SET @RetVal=(SELECT MAX(ID) FROM skyMSGAlarm WHERE Title=@ErrorMessage)  

END

 

ELSE

BEGIN

 

    DECLARE @MAX_ID INT

       SET @MAX_ID=(SELECT MAX(ID)+1 FROM skyMSGAlarm WHERE ID LIKE '93%') --<

       

      WHILE @MAX_ID IN (SELECT MAX(ID) FROM skyMSGAlarm)

      BEGIN

       SET @MAX_ID = @MAX_ID+1

      END

          

       SET @RetVal=@MAX_ID      

    INSERT INTO skyMSGAlarm(ID, Title)   

    SELECT @RetVal, @ErrorMessage 

 

END

 

RETURN @RetVal 

--*/  

      

 -------------------------------------            

    RAISERROR (@ErrorMessage,  -- Message text.

               @ErrorSeverity, -- Severity.

               @ErrorState     -- State.

               )  

  End Catch--结束捕捉

 

 

 

 

 

 

 

END

 

 

 

 

 

 

/*

ALTER FUNCTION dbo.fun_getVisableStr

(@str NVARCHAR(MAX)) 

RETURNS NVARCHAR(MAX) 

AS

BEGIN

 

   declare @word nchar(1),

           @VisableStr nvarchar(4000) 

       set @VisableStr=''

 

 WHILE LEN(@str)>0 

 BEGIN

 

   IF UNICODE( LEFT(@str,1) ) NOT BETWEEN 0 AND 31 AND UNICODE( LEFT(@str,1) )<>127

   SET @VisableStr=@VisableStr + LEFT(@str,1)

  

   SET @str=right(@str,LEN(@str)-1) 

  

 END

   SET @VisableStr= RTRIM( LTRIM(@VisableStr) )

 

RETURN @VisableStr

 

END

 

*--/

--*/

 

转载于:https://www.cnblogs.com/sthinker/p/5735250.html

你可能感兴趣的:(去除sql中不可见字符的n种方法)