我正在尝试清理公司数据库中的字符串(从我们进行更好的数据验证之前的早期开始)。但是,该功能无效。我发现它只是为@Pattern字符串分配'[',但我不确定原因。
重新创建函数的代码(您需要在函数名之前添加数据库前缀):
/* Remove select non-alphanumeric characters from a string */
CREATE FUNCTION [CleanString] (@Str VARCHAR(max))
RETURNS VARCHAR(max) AS
BEGIN
DECLARE @BadIndex SMALLINT, -- Index of bad character
@Pattern CHAR(132) -- Characters to look for (min length is # chars + 4 for %[]% enclosing)
SELECT @Pattern =
'%['
/* +CHAR(00) */ +CHAR(01) +CHAR(02) +CHAR(03) +CHAR(04) +CHAR(05) +CHAR(06) +CHAR(07) +CHAR(08) +CHAR(09)
+CHAR(10) +CHAR(11) +CHAR(12) +CHAR(13) +CHAR(14) +CHAR(15) +CHAR(16) +CHAR(17) +CHAR(18) +CHAR(19)
+CHAR(20) +CHAR(21) +CHAR(22) +CHAR(23) +CHAR(24) +CHAR(25) +CHAR(26) +CHAR(27) +CHAR(28) +CHAR(29)
+CHAR(30) +CHAR(31) /* +CHAR(32) */ +CHAR(33) +CHAR(34) +CHAR(35) +CHAR(36) /* +CHAR(37) */ +CHAR(38) +CHAR(39)
+CHAR(40) +CHAR(41) +CHAR(42) +CHAR(43) +CHAR(44) +CHAR(45) /* +CHAR(46) */ +CHAR(47) /* +CHAR(48) +CHAR(49)
+CHAR(50) +CHAR(51) +CHAR(52) +CHAR(53) +CHAR(54) +CHAR(55) +CHAR(56) +CHAR(57) */ +CHAR(58) +CHAR(59)
+CHAR(60) +CHAR(61) +CHAR(62) +CHAR(63) /* +CHAR(64) +CHAR(65) +CHAR(66) +CHAR(67) +CHAR(68) +CHAR(69)
+CHAR(70) +CHAR(71) +CHAR(72) +CHAR(73) +CHAR(74) +CHAR(75) +CHAR(76) +CHAR(77) +CHAR(78) +CHAR(79)
+CHAR(80) +CHAR(81) +CHAR(82) +CHAR(83) +CHAR(84) +CHAR(85) +CHAR(86) +CHAR(87) +CHAR(88) +CHAR(89)
+CHAR(90) +CHAR(91) */ +CHAR(92) /* +CHAR(93) */ +CHAR(94) /* +CHAR(95) */ +CHAR(96) /* +CHAR(97) +CHAR(98) +CHAR(99)
+CHAR(100) +CHAR(101) +CHAR(102) +CHAR(103) +CHAR(104) +CHAR(105) +CHAR(106) +CHAR(107) +CHAR(108) +CHAR(109)
+CHAR(110) +CHAR(111) +CHAR(112) +CHAR(113) +CHAR(114) +CHAR(115) +CHAR(116) +CHAR(117) +CHAR(118) +CHAR(119)
+CHAR(120) +CHAR(121) +CHAR(122) */ +CHAR(123) +CHAR(124) +CHAR(125) +CHAR(126) +CHAR(127)
+']%',
@BadIndex = PATINDEX(@Pattern, @Str) -- Find first bad character index
WHILE @BadIndex > 0
BEGIN
SELECT
@Str = STUFF(@Str, @BadIndex, 1, ' ') -- Swap bad char for a space
,@BadIndex = PATINDEX(@Pattern, @Str) -- Find next bad character index
END
RETURN @Str
END
测试(您需要在函数名称前添加数据库前缀):
select CleanString('woqp ienv8 90()*&@ #$! skllj_asdf') as test
结果:
'woqp ienv8 90()*&@ #$! skllj_asdf'
提前感谢您的帮助:)
答案 0 :(得分:2)
我注意到有两件事。首先,@patindex
应该有通配符。其次,你可能不想在循环内返回。
您可以尝试这样的逻辑:
SET @Pattern = '%' + @Pattern + '%';
SET @BadIndex = PATINDEX(@Pattern, @Str); -- Find first bad character index
WHILE @BadIndex > 0
SELECT @Str = STUFF(@Str, @BadIndex, 1, ' '), -- Swap bad char for a space
@BadIndex = PATINDEX(@Pattern, @Str); -- Find next bad character index
END;
RETURN @Str;
编辑:
问题在于您的declare
声明。您已将模式声明为char()
而不是varchar()
。这意味着图案在右侧填充了空格,并且图案永远不会匹配。
只需改变一下:
DECLARE @BadIndex SMALLINT, -- Index of bad character
@Pattern CHAR(132) -- Characters to look for (min length is # chars + 4 for %[]% enclosing)
要:
DECLARE @BadIndex SMALLINT, -- Index of bad character
@Pattern VARCHAR(8000) -- Characters to look for (min length is # chars + 4 for %[]% enclosing)
这一次我测试了它并且有效。