邮件数据:
Mailid
-------------------------------
venkattaramanan1985@gmail.com
madanraj@gmail.com
预期输出
Name DomainName Extension
------------------- ---------- ---------
venkattaramanan1985 gmail com
madanraj gmail com
答案 0 :(得分:6)
它有一个简单的单行解决方案,假设电子邮件地址为 admin@system.org ,下面的查询将返回如上所述的部分:
SELECT
--admin
LEFT(emailAddres, CHARINDEX('@', emailAddres) - 1) AS accountName
--system.org
RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres)) AS domainWithExtension
--system
LEFT(RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres)), CHARINDEX('.', RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres))) - 1) AS domain
--org
RIGHT(RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres)), LEN(RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres))) - CHARINDEX('.', RIGHT(emailAddres, LEN(emailAddres) - CHARINDEX('@', emailAddres)))) AS extension
希望得到这个帮助。
答案 1 :(得分:3)
首先,为什么使用SQL服务器?
我建议使用客户端进行字符串操作,然后让SQL服务器返回数据。这应该是SQL服务器的唯一工作。
如果你真的必须使用SQL服务器,你可能想要事先阅读SQL中的split字符串。 (性能比较阅读here)
对于以下提出的解决方案,您需要一个table-value-function来拆分带有指定分隔符的传递字符串,该分隔符返回有序的子字符串。我修改了从here获取的公用表表达式以返回排序。
CREATE FUNCTION dbo.SplitStrings_CTE
(
@List NVARCHAR(MAX),
@Delimiter NVARCHAR(255)
)
RETURNS @Items TABLE ([Order] INT IDENTITY(1,1) NOT NULL, [Item] NVARCHAR(4000))
WITH SCHEMABINDING
AS
BEGIN
--same as the original here, the ordering is inserted automatically by IDENTITY
(...)
END
现在我添加了一个函数来重新分割电子邮件字符串,重新使用上面的函数:
CREATE FUNCTION dbo.SplitEmail
(
@email NVARCHAR(254)
)
RETURNS @splitted TABLE (
[Email] NVARCHAR(254),
[Name] NVARCHAR(254),
[DomainName] NVARCHAR(254),
[Extension] NVARCHAR(254)
)
WITH SCHEMABINDING
AS
BEGIN
DECLARE @name NVARCHAR(254), @domain NVARCHAR(254), @ext NVARCHAR(254)
--init the email parts with defaults
SELECT @name = ISNULL(@email, ''), @domain = '', @ext = '';
--we only want to split @email if at least one '@' is found in it
IF (@email IS NOT NULL AND LEN(@email) <> 0 AND CHARINDEX('@', @email) <> 0) BEGIN
--take the last occuring substring from @email as the @domain
SELECT TOP 1 @domain = [Item]
FROM [dbo].[SplitStrings_CTE](@name, '@')
ORDER BY [Order] DESC
--@email without @domain + '@' is the @name
SET @name = LEFT(@name, LEN(@name) - LEN(@domain) - 1)
--we only want to split @domain if at least one '.' is found in it
IF (CHARINDEX('.', @domain) <> 0) BEGIN
--take the last occuring substring from @domain as the @ext
SELECT TOP 1 @ext = [Item]
FROM [dbo].[SplitStrings_CTE](@domain, '.')
ORDER BY [Order] DESC
--split the @ext from the @domain
SET @domain = LEFT(@domain, LEN(@domain) - LEN(@ext) - 1)
END
ELSE BEGIN
SET @ext = @domain
SET @domain = ''
END
END
INSERT INTO @splitted ([Email], [Name], [DomainName], [Extension])
VALUES (@email, @name, @domain, @ext)
RETURN
END
调用该函数 - 例如:
DECLARE @@samples TABLE([mailid] NVARCHAR(255))
INSERT INTO @@samples ([mailid]) VALUES ('venkattaramanan1985@gmail.com')
INSERT INTO @@samples ([mailid]) VALUES ('madanraj@gmail.com')
INSERT INTO @@samples ([mailid]) VALUES ('madanraj@sub.gmail.com')
INSERT INTO @@samples ([mailid]) VALUES ('sm@al.l')
INSERT INTO @@samples ([mailid]) VALUES ('is@sane.')
INSERT INTO @@samples ([mailid]) VALUES ('is@sane')
INSERT INTO @@samples ([mailid]) VALUES ('rubbish@h.h.h.h.h')
INSERT INTO @@samples ([mailid]) VALUES (NULL)
INSERT INTO @@samples ([mailid]) VALUES ('')
INSERT INTO @@samples ([mailid]) VALUES ('invalid')
INSERT INTO @@samples ([mailid]) VALUES ('@@@@@@@@@...')
SELECT e.*
FROM @@samples AS s
CROSS APPLY dbo.SplitEmail(s.mailid) AS e
将返回此结果:
Email Name DomainName Extension
--------------------------------- ---------------------- ---------------- ------------
venkattaramanan1985@gmail.com venkattaramanan1985 gmail com
madanraj@gmail.com madanraj gmail com
madanraj@sub.gmail.com madanraj sub.gmail com
sm@al.l sm al l
is@sane. is sane
is@sane is sane
rubbish@h.h.h.h.h rubbish h.h.h.h h
NULL
invalid invalid
@@@@@@@@@... @@@@@@@@ ..
答案 2 :(得分:1)
declare @var varchar(50) ='Abc.xyz@rediffmail.com';
select
SUBSTRING(@var,0,charindex('@',@var)) as UserName
,
Substring(@var,charindex('@',@var)+1,charindex('.',@var,charindex('@',@var,0))-(charindex('@',@var)+1)) as DomainName
,
substring(@var,charindex('.',@var,charindex('@',@var))+1,len(@var)) as Ext
答案 3 :(得分:0)
我看到 OP 将请求标记为 sql-server。我发现这个在找mysql,去图。所以这是一个mysql答案。
SUBSTRING_INDEX('john.doe@simon.says.com', '@', 1) AS Name,
SUBSTRING_INDEX(SUBSTRING_INDEX('john.doe@simon.says.com', '@', -1), '.', ROUND((LENGTH(SUBSTRING_INDEX('john.doe@simon.says.com', '@', -1)) - LENGTH(REPLACE(SUBSTRING_INDEX('john.doe@simon.says.com', '@', -1), '.', ''))) / LENGTH('.'))) AS DomainName,
SUBSTRING_INDEX('john.doe@simon.says.com', '.', -1) AS Extension,
最终结果将返回: