更新scipt以从“分发列表”列中删除电子邮件地址

时间:2018-01-25 09:44:16

标签: sql regex sql-server-2016

我有一个表格列如下:

DROP TABLE IF EXISTS #EXAMPLE;
CREATE TABLE #EXAMPLE(DistributionList VARCHAR(250));

INSERT INTO #EXAMPLE
values
('x@x.com; y@y.co.uk; me@me.com;'), 
('me@me.com; a@a.com;'), 
('me@me.com;'), 
('NULL'), 
('z@z.com; me@me.com;'), 
('NULL'), 
('c@c.co.uk; me@me.com; xx@x.com;'), 
('NULL');

SELECT *
FROM   #EXAMPLE;

如何剥离' me@me.com'来自列中的每个值?

1 个答案:

答案 0 :(得分:1)

尝试 REPLACE

UPDATE #EXAMPLE 
SET DistributionList = NULLIF(NULLIF(REPLACE(DistributionList, 'me@me.com;', ''), ''), ';')
FROM #EXAMPLE
WHERE DistributionList like '%me@me.com%'