IF OBJECT_ID('dbo.fn_fco_valida_nombre') IS NOT NULL
    DROP FUNCTION dbo.fn_fco_valida_nombre
GO
CREATE FUNCTION dbo.fn_fco_valida_nombre(@pvc_nombre varchar(30))
RETURNS varchar(30)
AS
BEGIN
    DECLARE 
        @vvc_limpio varchar(30), 
        @i tinyint, 
        @vch_car char(1)

    SET @vvc_limpio = Replace(@pvc_nombre, 'ñ', 'n')
    SET @vvc_limpio = Replace(@vvc_limpio, 'á', 'a')
    SET @vvc_limpio = Replace(@vvc_limpio, 'é', 'e')
    SET @vvc_limpio = Replace(@vvc_limpio, 'í', 'i')
    SET @vvc_limpio = Replace(@vvc_limpio, 'ó', 'o')
    SET @vvc_limpio = Replace(@vvc_limpio, 'ú', 'u')

    SET @i = 1
    WHILE @i <= LEN(@vvc_limpio)
    begin
        SET @vch_car = SUBSTRING(@vvc_limpio, @i, 1)

        IF @vch_car NOT LIKE '[a-Z]'
            SET @vvc_limpio = REPLACE(@vvc_limpio, @vch_car, ' ')

        SET @i = @i + 1
    end

    RETURN @vvc_limpio
END
GO
GRANT EXECUTE ON dbo.fn_fco_valida_nombre TO PUBLIC
GO
