Função em linguagem Transact-SQL, a linguagem de programação do Microsoft SQL Server, para extrair somente os números de uma string.
Funcionamento:
+55 (21) 98346-3683 -> 5521983463683
972.347.321-12 -> 97234732112
CREATE FUNCTION dbo.GetNumeric
(
@strAlphaNumeric VARCHAR(256)
)
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN ISNULL(@strAlphaNumeric,0)
END
GOExemplo de uso:
UPDATE Cadastro SET Indice = dbo.GetNumeric(Telefone) SELECT dbo.GetNumeric(CPF) AS SomenteNumeros FROM Cadastro WHERE Cidade = 'Rio de Janeiro'
Se achou útil, considere assinar este blog preechendo seu e-mail no campo abaixo.