如何从SQLServer中的字符串中删除所有非字母字符?如何从字符串中删除所有非字母字符?那非字母数字呢?这必须是一个自定义函数,还是也有更多可通用的解决方案?
3 回答
jeck猫
TA贡献1909条经验 获得超7个赞
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
TA贡献1803条经验 获得超6个赞
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
TA贡献1786条经验 获得超11个赞
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
添加回答
举报
0/150
提交
取消