我有一个电子邮件列,连续存储至少10封以上的电子邮件。现在,我想写一个查询,将每封电子邮件放在一个单独的行上,例如:
hay@line.com
u@y.com
live.gmail.com
如何写这个?
答案 0 :(得分:2)
如果您的意思是数据行...如果您没有2016年,任何解析/拆分功能都会执行。否则,REPLACE()就像JohnHC所提到的那样
Declare @YourTable table (ID int,Emails varchar(max))
Insert Into @YourTable values
(1,'hay@line.com,u@y.com,live.gmail.com')
Select A.ID
,EMail=B.RetVal
From @YourTable A
Cross Apply [dbo].[udf-Str-Parse](A.EMails,',') B
返回
ID EMail
1 hay@line.com
1 u@y.com
1 live.gmail.com
或者只是
Select * from [dbo].[udf-Str-Parse]('hay@line.com,u@y.com,live.gmail.com',',')
返回
RetSeq RetVal
1 hay@line.com
2 u@y.com
3 live.gmail.com
需要的功能
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ Replace(@String,@Delimiter,'</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
答案 1 :(得分:1)
select replace(MyEmailField, '<CurrentDelimeter>', char(13)) as NewEmail
from MyTable