Below is the function:
create function dbo.CleanTheStringAdv (@theString as nvarchar(500), @CleanMode as nvarchar(500))
returns nvarchar(500)
AS
begin
--define variables
declare @strAlphaNumeric nvarchar(500)
declare @i int
declare @strChar nvarchar(500)
declare @CleanedString nvarchar(500)
--initiate values
set @CleanedString =''
set @theString = @theString + ''
SET @i = 1
--Determine if we are looking for numeric values only or numbers and letters
SET @strAlphaNumeric = case @CleanMode
--case @CleanMode
WHEN '1'
THEN '0123456789'
WHEN '2'
THEN '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
WHEN '3'
THEN '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ|'
else 0
end
--Search through chars in the string passed to the function
while (@i <= Len(@theString))
begin
set @strChar = substring(@theString, @i, 1)
--if the current char being reviewed is valid then add it to the new string
If (charindex(@strChar, @strAlphaNumeric) <>0)
begin
set @CleanedString = @CleanedString + @strChar
end
end
return @CleanedString
end
---------
when I run, select dbo.CleanTheStringAdv('100651|210110P3444470','1')
it took nearly 14 hours and didnt yet return any output.
when I run, select dbo.CleanTheStringAdv('100651|210110P3444470','3')
below error message is seen:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ|' to data type int.
Any help is highly appreciated........