简体   繁体   English

SQL 代码查找和抑制数字和特殊字符

[英]SQL code to find and suppress numbers and special characters

I want to include/modify in my SQL code to be able to eliminate the following characters: '&% -single quotes -ampersand -percentage我想在我的 SQL 代码中包含/修改,以便能够消除以下字符:'&% -single quotes -ampersand -percentage

I need to include any of this characters in the current REGEX.我需要在当前的正则表达式中包含任何这些字符。 Thanks in advance提前致谢

DECLARE @counter int;
SET @counter = 0
WHILE(@counter < (SELECT MAX(ID_COLUMN) FROM [dbo].[zzIntegriyCheckV4HistoryClient_TEMP]))

BEGIN  
    IF  EXISTS (SELECT * FROM  [dbo].[zzIntegriyCheckV4HistoryClient_TEMP] WHERE MESSAGE NOT LIKE '% must be between %')
    WHILE 1 = 1
    BEGIN

        DECLARE @RetVal varchar(50)

        SET @RetVal =  (SELECT MESSAGE = STUFF(MESSAGE, PATINDEX('%[0-9()$%&/.:!]%', MESSAGE),1, '')
        FROM [dbo].[zzIntegriyCheckV4HistoryClient_TEMP]
        WHERE ID_COLUMN = @counter)

        IF(@RetVal IS NOT NULL)       
          UPDATE [dbo].[zzIntegriyCheckV4HistoryClient_TEMP] SET MESSAGE = @RetVal WHERE ID_COLUMN = @counter
        ELSE
            break
    END

    SET @counter = @counter + 1
END

This is a blind shot as you did not specify your RDBMS.这是一个盲目的镜头,因为您没有指定您的 RDBMS。 But assuming your [tsql] is pointing to SQL-Server and you have a version v2017+, you might try this:但假设您的[tsql]指向 SQL-Server 并且您的版本为 v2017+,您可以试试这个:

--some muddy input --一些泥泞的输入

DECLARE @input VARCHAR(100)='abc123(33)%&$.def,,3xyz';

--a list of all characters you want to get rid of --您要删除的所有字符的列表

DECLARE @notWanted VARCHAR(100)='0123456789!"§$%&/()=?*+-_#''<>@€,.;:';

--an intermediate placeholder (any of the forbidden characters) --一个中间占位符(任何禁止字符)

DECLARE @useInstead CHAR(1)='~';

--Here you see the work of TRANSLATE() --这里你看到了TRANSLATE()的工作

SELECT TRANSLATE(@input,@notWanted,REPLICATE(@useInstead,LEN(@notWanted)));
--> abc~~~~~~~~~~~~def~~~xyz

--Now you can use a simple REPLACE() --现在你可以使用一个简单的 REPLACE()

SELECT REPLACE(TRANSLATE(@input,@notWanted,REPLICATE(@useInstead,LEN(@notWanted))),@useInstead,'')
--> abcdefxyz

Hint: Before anybody starts to offer a huge answer with a recursive CTE, loops, tally lists and so on please provide more details.提示:在任何人开始使用递归 CTE、循环、计数列表等提供大量答案之前,请提供更多详细信息。
You might read this answer where I provided a function to deal with each character separately.您可能会阅读此答案,其中我提供了 function 来分别处理每个字符。 You can adapt this to your needs.您可以根据自己的需要进行调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle SQL 查询查找电话号码的特殊字符 - Oracle SQL query to find special characters for phone numbers 识别 SQL 中的特殊字符和数字 - Identify Special Characters and numbers in SQL 如何在 Oracle SQL 中找到 col 具有特殊字符或数字(连字符、撇号和空格除外)的行 - How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL SQL Select语句(REGEXP)在仅alpha字段中查找特殊字符和数字 - SQL Select Statement(REGEXP) to find special characters and numbers in an alpha only field 如何在 Oracle SQL 中找到 col 有字母、数字或特殊字符(连字符、撇号和空格除外)的行 - How to find a row where col have alphabets,numbers or special characters (except hyphen,apostrophe and space) in Oracle SQL 用于排除名称末尾的数字、括号或特殊字符的 SQL 查询 - SQL query to exclude numbers, brackets or special characters at the end of a name 如何在一个oracle sql语句中删除空格、特殊字符和数字 - how to remove spaces, special characters and numbers in one oracle sql statement 在 T-SQL 中排除特殊字符并提取一组数字 - Exclude special characters and extract a set of numbers in T-SQL 使用SQL查找带有意外字符的电话号码 - Find phone numbers with unexpected characters using SQL 选择LIKE或CHARINDEX()函数以使用SQL查找一些特殊的Unicode字符 - Select LIKE or CHARINDEX() function to find some special unicode characters with SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM