我在员工表中有一个电子邮件列值:
Email
regan.manning@cresa.com
miang.luso@praxis.com
selin.robert@cummins.com
我想将电子邮件拆分为三列,如:
FirstName LastName DomainName
--------------------------------------------------
regan manning @cresa.com
miang luso @praxis.com
selin robert @cummins.com
答案 0 :(得分:2)
一种方法使用apply
:
select t.*, v.domain, v2.firstname, v2.lastname
from t cross apply
(values(stuff(email, 1, charindex('@', email), '') as domain,
left(email, charindex('@', email)
)
) v(domain, name) cross apply
(values (left(name, charindex('.')),
stuff(name, 1, charindex('.', name), '')
)
) v2(lastname, firstname;
答案 1 :(得分:0)
SELECT
substring( @email , 0 , CHARINDEX('.' , @email) ) ,
substring( @email , CHARINDEX('.',@email)+1 , CHARINDEX('@' , @email) - CHARINDEX('.' , @email)-1 ),
substring( @email , CHARINDEX('@' , @email) , LEN(@email) - CHARINDEX('@' , @email)+1 )
from employee;
- 将@email替换为您的电子邮件列的名称
更容易理解的版本:
DECLARE @email NVARCHAR(30) = 'selin.robert@cummins.com';
DECLARE @dot INT =CHARINDEX( '.' , @email );
DECLARE @atrate INT =CHARINDEX( '@' , @email );
DECLARE @len INT =LEN( @email );
SELECT
substring( @email , 0 , @dot ),
substring( @email , @dot+1 , @atrate - @dot-1 ),
substring( @email, @atrate , @len - @atrate+1 );
答案 2 :(得分:0)
使用Substring
& Charindex
功能检查.
,@
&检索数据:
SELECT @email [Email],
SUBSTRING(@email, 1, CHARINDEX('.', @email)-1) [FirstName],
SUBSTRING(@email, CHARINDEX('.', @email)+1, CHARINDEX('@', @email)-CHARINDEX('.', @email)-1) [LastName],
SUBSTRING(@email, CHARINDEX('@', @email), LEN(@email)) [DomainName] from <table_name>;
结果:
Email FirstName LastName DomainName
miang.luso@praxis.com miang luso @praxis.com