Created
August 19, 2020 12:06
-
-
Save ebenito/e0ee0fcce9a8188f6b275f53758ebe39 to your computer and use it in GitHub Desktop.
TSQL - Extraer correo electronico de un texto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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