使用MS SQL 2008,我需要洗掉包含在一串文本中的電子郵件地址。 即:
"這是一個包含多個電子郵件地址的文本樣本行,如[email protected],或者我也可以有[email protected],甚至[email protected],以混淆視聽"。
期望的結果是:
"這是一個帶有多個電子郵件地址的文本樣本行,如或我也可以有或甚至是混合的電子郵件地址"
或者甚至:
"這是一個帶有多個電子郵件地址的文本樣本行,如fred@*****,或者我也可以有bert@*****,甚至是某人@*****,以混合各種情況"。
有很多洗掉某個字符左邊或右邊的所有內容的例子,但沒有洗掉某個固定字符左邊或右邊的文字直至第一個空格的例子。希望得到任何幫助。
uj5u.com熱心網友回復:
假設文本字串在一個表列中,你可以在SQL Server 2008中通過for xml的字串吐字函式和字串連接的幫助下完成這個任務。 在較新的SQL Server版本中--你真的應該嘗試遷移到該版本中--這可以通過string_agg等函式而不是stuff(...for xml )組合來實作。
這個植入是貪婪的,因為它將屏蔽兩個空格(或字串的開始/結束)之間的任何字符組合,其中至少包括一個@字符。 因此,fullsentences-like^this'one% will£$included,solong)as/there>is.a=@*character&somewhere以及僅由一個或多個@字符組成的字串。
我將把如何處理不是電子郵件地址的字串留給你來決定。
查詢
with t as
(
select *
from(values('這是一行有多個電子郵件地址的示例文本,如[email protected],或者我也可以有[email protected],甚至[email protected],以混淆事情')
,('這是另一個文本樣本行,有多個電子郵件地址,如[email protected],或者我也可以有[email protected],甚至[email protected],以混合事情。')
,('fullsentences-like^this''one%will£be$included,solong)as/there>is.a=@*character&somewhere')
,('@')
,('a @@@@@ b')
,('@ @ @ @')
,('Let's meet @ the beach')
) as s(s)
)
,s as
(
select t.s
,s.rn
,case when charindex('@'/span>,s. item) > 0 then '***'/span> else s. 專案 end as 專案
from t
cross apply dbo.fn_StringSplit4k(t.s,' ' ,null) as s
)
select t.s
,stuff((select ' ' s.item)
from s
where t.s = s.s
order by s.rn
for xml path(''/span>)
)
,1,1,'')
) as s
from t。
輸出
字串拆分函式
create function [dbo].[fn_StringSplit4k]
(
@str nvarchar(4000) = ' ' --要分割的字串。
,@delimiter as nvarchar(1) = ' 。 ' --要分割的限定值。
,@num as int = null --回傳哪個值。
)
returns table
as
return
--以10行開始統計表。
with n(n) as (select1 union all select 1 union all all select 1 union all all select select 1 union all select 1 1 union all select 1 union all select 1 union all all select 1 union all all select select 1 union all select 1)
--選擇與@str中的字符相同數量的行作為增量行號。
-- 交叉連接以指數方式增加到最大可能的10,000行以覆寫最大的@str長度。
,t(t) as (select top (select len(isnull(@str, '') a) row_number() over (order by (select null) from n n1,n n2,n n3,n n4)
-- 回傳每一個跟在指定分隔符后面的數值的位置。
, s(s) as (select 1 union all select t 1 from t where substring(isnull(@str, ''/span>),t,1) = @delimiter)
--回傳每個值的起始和長度,以便在SUBSTRING函式中使用。
-- ISNULL/NULLIF組合處理字串末端沒有定界符的最后一個值。
,l(s,l) as (select s,isnull(nullif>(charindex(@delimiter, isnull(@str,''),s),0)-s,4000) from s)
select rn
,專案
from(select row_number() over(order by s) as rn
,substring(@str,s,l) 作為 item
from l
) a
where rn = @num
or @num is null;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314536.html
標籤:
