Remove signs from a string in SQL Server

2

I have a question about an alphanumeric chain, I have to validate that this chain if it has an alphanumeric, for example:

EJEMPLO123+

Delete the% "+" and keep the alphanumeric only.

    
asked by Ab Hdez 26.01.2018 в 00:42
source

1 answer

3

You could create a function that eliminates the characters that do not interest you.

CREATE FUNCTION dbo.EliminaNoAlfanumericos(@variable varchar(100)) RETURNS VARCHAR(100)
BEGIN
    DECLARE @posicion INT
    SET @posicion = PATINDEX('%[^A-Za-z0-9]%',@variable )
    WHILE @posicion > 0
       BEGIN
          SET @variable = STUFF(@variable ,@posicion ,1,'')
          SET @posicion = PATINDEX('%[^A-Za-z0-9]%',@variable )
       END
    RETURN @variable
END
GO

The function what is doing is a loop looking, with PATINDEX , the non-alphanumeric characters that contains the @variable until none is left, and using the function STUFF , is replacing by white '' the character found in @posición within @variable .

    
answered by 26.01.2018 / 09:59
source