-
jeck猫
尝试以下功能:Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))Returns VarChar(1000)ASBegin
Declare @KeepValues as varchar(50)
Set @KeepValues = '%[^a-z]%'
While PatIndex(@KeepValues, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
Return @TempEnd就这样说吧:Select dbo.RemoveNonAlphaCharacters('abc1234def5678ghi90jkl')一旦您理解了代码,您就会发现更改它以删除其他字符也是相对简单的。您甚至可以使这个动态通过您的搜索模式。希望能帮上忙。
-
慕码人8056858
参数化版本G型天象' 可怕的答案:CREATE FUNCTION [dbo].[fn_StripCharacters](
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255))RETURNS NVARCHAR(MAX)ASBEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @StringEND只按字母顺序排列:SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z')仅为数字:SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^0-9')只有字母数字:SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z0-9')非字母数字:SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', 'a-z0-9')
-
Qyouu
信不信由你,在我的系统中,这个丑陋的功能比G天象优雅的功能表现得更好。CREATE FUNCTION dbo.RemoveSpecialChar (@s VARCHAR(256)) RETURNS VARCHAR(256) WITH SCHEMABINDING BEGIN
IF @s IS NULL
RETURN NULL
DECLARE @s2 VARCHAR(256) = '',
@l INT = LEN(@s),
@p INT = 1
WHILE @p <= @l BEGIN
DECLARE @c INT SET @c = ASCII(SUBSTRING(@s, @p, 1))
IF @c BETWEEN 48 AND 57
OR @c BETWEEN 65 AND 90
OR @c BETWEEN 97 AND 122
SET @s2 = @s2 + CHAR(@c)
SET @p = @p + 1
END
IF LEN(@s2) = 0
RETURN NULL
RETURN @s2