我有一个功能,基本上从选择中删除数字。此功能适用于基于英语的字符集。我有一组新的数据,包括非英文字符(中文,日文,印度等),但我没有达到预期的效果。我使用NVarchar
集来存储任何非英语记录。以下是我目前使用的功能。任何建议都会非常有用。
GO
/****** Object: UserDefinedFunction [dbo].[StripVenName] Script Date: 05/03/2012 16:24:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[StripVenName] (@InString as VarChar(8000))
RETURNS NVarChar(100) AS
BEGIN
Declare @ReturnVal as NVarchar(4000)
Declare @OutString as NVarchar(4000)
Declare @Pos as smallint
Declare @CurChar as NVarChar(1)
IF LEN(@InString) = 0
SET @ReturnVal = ''
ELSE
BEGIN
Set @Pos = 1
SET @OutString = ' '
WHILE (@Pos <= Len(@InString))
BEGIN
Set @CurChar = SUBSTRING(@InString, @Pos, 1)
if ASCII(@CurChar) between 65 and 90 or ASCII(@CurChar) between 97 and 122
SET @OutString = @OutString + @CurChar
Set @Pos = @Pos + 1
END
if len(@OutString) = 0
SET @OutString = @InString
SET @ReturnVal = LTRIM(RTRIM(@OutString))
END
RETURN @ReturnVal
END
答案 0 :(得分:0)
您的代码可以减少到此(并且它也会表现得更好)。
Create Function [dbo].[RemoveNumericCharacters](@Temp NVarChar(1000))
Returns NVarChar(1000)
AS
Begin
While PatIndex(N'%[0-9]%', @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(N'%[0-9]%', @Temp), 1, N'')
Return @Temp
End