TSQL - 如何从一串文本中去除借记卡号码?

Ton*_*ony 4 sql-server t-sql

我们有一个表格,其中存储了一大串文本,其中包含一个借记卡号码,该文本串是从客户发送的电子邮件中复制粘贴的。

如何搜索 7000 多条记录并识别/替换从0000-0000-0000-0000到的卡号XXXX-XXXX-XXXX-0000

Dav*_*itz 6

这可以每行处理一张信用卡。如果您怀疑可能有 1 个以上,则只需多次执行 UPDATE(在演示中我将它作为单独的语句运行,但您可以使用GO X

declare @mycol varchar(1000) = 'Hello! my name is 0000-0000-0000-0000 Inigo Montoya'

select  stuff(@mycol,patindex('%[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%',@mycol),14,'xxxx-xxxx-xxxx')
Run Code Online (Sandbox Code Playgroud)

演示

create table #mytable (id int,mycol varchar(max));

insert into #mytable (id,mycol) values
    (1,'Hello! my name is 1234-2345-3456-4567 Inigo Montoya')
   ,(2,'Please continue, 1234-5678, there is nothing to see')
   ,(3,'the 1st one is 1111-2222-3333-4444 and the 2nd is 2222-3333-4444-5555. That''s it')
;

update  t
set     mycol = stuff(mycol,patindex('%[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%',mycol),14,'xxxx-xxxx-xxxx')
from    #mytable as t
where   patindex('%[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%',mycol) > 0
;
Run Code Online (Sandbox Code Playgroud)

(受影响的 2 行)

select * from #mytable;

+----+----------------------------------------------------------------------------------+
| id | mycol                                                                            |
+----+----------------------------------------------------------------------------------+
| 1  | Hello! my name is xxxx-xxxx-xxxx-4567 Inigo Montoya                              |
+----+----------------------------------------------------------------------------------+
| 2  | Please continue, 1234-5678, there is nothing to see                              |
+----+----------------------------------------------------------------------------------+
| 3  | the 1st one is xxxx-xxxx-xxxx-4444 and the 2nd is 2222-3333-4444-5555. That's it |
+----+----------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
update  t
set     mycol = stuff(mycol,patindex('%[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%',mycol),14,'xxxx-xxxx-xxxx')
from    #mytable as t
where   patindex('%[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%',mycol) > 0
;
Run Code Online (Sandbox Code Playgroud)

(1 行受影响)

select * from #mytable;


+----+----------------------------------------------------------------------------------+
| id | mycol                                                                            |
+----+----------------------------------------------------------------------------------+
| 1  | Hello! my name is xxxx-xxxx-xxxx-4567 Inigo Montoya                              |
+----+----------------------------------------------------------------------------------+
| 2  | Please continue, 1234-5678, there is nothing to see                              |
+----+----------------------------------------------------------------------------------+
| 3  | the 1st one is xxxx-xxxx-xxxx-4444 and the 2nd is xxxx-xxxx-xxxx-5555. That's it |
+----+----------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)