输入: -
column email:
kiran.paluri@live.com
shree.lavanya@gmail.com
krsna.kumar@yahoo.com
我希望输出为
firstname lastname domain
kiran paluri live
shree lavanya gmail
krsna kumar yahoo
答案 0 :(得分:0)
下面是一个可以为您解析的函数,如果域之前没有句点,则会考虑该函数。以下是如何利用它的一个例子。
正如戈登提到的那样,我不确定你引用了哪个数据库,所以我为了这个答案而假设SQL Server。
CREATE FUNCTION dbo.splitEmail ( @email VARCHAR(MAX) )
RETURNS
@ret TABLE ([FirstName] VARCHAR(MAX), [LastName] VARCHAR(MAX), [Domain] VARCHAR(MAX))
AS
BEGIN
DECLARE @dotIndex INT, @atIndex INT
SELECT @atIndex=CHARINDEX('@',@email)
SELECT @dotIndex=CHARINDEX('.', LEFT(@email, @atIndex))
DECLARE @first VARCHAR(MAX), @last VARCHAR(MAX), @domain VARCHAR(MAX)
SELECT @first = CASE WHEN @dotIndex > 0 THEN SUBSTRING(@email, 0, @dotIndex) ELSE SUBSTRING(@email, 0,@atIndex) END
SELECT @last = CASE WHEN @dotIndex > 0 THEN SUBSTRING(@email, @dotIndex + 1, @atIndex - @dotIndex - 1) ELSE NULL END
SELECT @domain = SUBSTRING(@email, @atIndex + 1, LEN(@email) - @atIndex)
INSERT INTO @ret([FirstName],[LastName],[Domain])
VALUES (@first, @last, @domain)
RETURN
END
使用该功能:
DECLARE @tbl TABLE (email VARCHAR(MAX))
INSERT INTO @tbl VALUES ('first.last@domain.com')
INSERT INTO @tbl VALUES ('john.smith@overthere.com')
INSERT INTO @tbl VALUES ('firstonly@contoso.com')
SELECT *
FROM @tbl AS t
CROSS APPLY dbo.splitEmail(t.email)