I'm using this code to remove non numeric characters from a string:
ALTER FUNCTION [dbo].[StripNonNumerics](@Temp VARCHAR(255))
RETURNS VARCHAR
AS
BEGIN
DECLARE @KeepValues AS VARCHAR(50);
SET @KeepValues = '%[^0-9]%';
WHILE PATINDEX(@KeepValues, @Temp) > 0
BEGIN
SET @Temp = STUFF(@Temp, PATINDEX(@KeepValues, @Temp), 1, '');
END
RETURN @Temp
END;
I expect that if I call this function, putting as input a string like this one:
select [dbo].[StripNonNumerics]('vAn34nd2')
I could get this output:
342
Instead, I obtain:
3
I changed input to test this behaviour, and I noticed that it takes only the first non numeric characters sequence. Could you tell me what I'm overlooking?
Thanks
varcharvariables and parameters that you use. Otherwise, they might end up being a default length, which in case of a parameter or return value is 1 CHARACTER .....