Skip to content

Instantly share code, notes, and snippets.

@ebenito
Created August 19, 2020 12:06
Show Gist options
  • Save ebenito/e0ee0fcce9a8188f6b275f53758ebe39 to your computer and use it in GitHub Desktop.
Save ebenito/e0ee0fcce9a8188f6b275f53758ebe39 to your computer and use it in GitHub Desktop.
TSQL - Extraer correo electronico de un texto
CREATE FUNCTION [dbo].[fnExtraerEmails] (@email varchar(255))
RETURNS VARCHAR(255)
AS
BEGIN
declare @Izda varchar(255);
declare @RevIzda varchar(255);
declare @Dcha varchar(255);
declare @resultado varchar(255);
SET @Izda = LEFT(@email, CHARINDEX('@', @email, 0) - 1);
SET @RevIzda = REVERSE(LEFT(@email, CHARINDEX('@', @email, 0) - 1));
SET @Dcha = RIGHT(@email, LEN(@email) - CHARINDEX('@', @email, 0) + 1 )
SET @resultado =
REVERSE(
SUBSTRING(@RevIzda, 0,
CASE
WHEN PATINDEX('%[' + CHAR(10)+'- ]%', @RevIzda) = 0 THEN LEN(@RevIzda) + 1
ELSE PATINDEX('%[' + CHAR(0)+'- ]%', @RevIzda)
END
)
)
+
SUBSTRING(@Dcha, 0,
CASE
WHEN PATINDEX('%[' + CHAR(0)+'- ]%', @Dcha) = 0 THEN LEN(@Dcha) + 1
ELSE PATINDEX('%[' + CHAR(0)+'- ]%', @Dcha)
END
)
RETURN @resultado
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment