[英]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.