下面的函数将从字符串中删除其他非字母字符。
IF object_id('dbo.GetAlphachars') is not NULL
DROP FUNCTION dbo.GetNumeric
GO
CREATE FUNCTION dbo.GetAlphachars (@strAlphachars VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^a-z]%', @strAlphachars )
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphachars = STUFF(@strAlphachars, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^a-z]%', @strAlphachars )
END
END
RETURN ISNULL(@strAlphachars ,0)
END
GO
例子。
CREATE table vish(val varchar(20));
INSERT into vish values('.,,.?ishhh>...,');
SELECT dbo.getalphachars(val) from vish;
result
------------
ishhh