我需要对包含超过100万条记录的表进行更新,以掩盖信用卡。
我必须保留最后4位数,其余的应该是X的。
到目前为止我想出了这个
BEGIN TRAN
-- SQL update in batches of 10,000
WHILE (2 > 1)
BEGIN
BEGIN TRANSACTION
UPDATE TOP ( 10000 ) tblMyTable
SET creditCardNumber = 'XXXXXX' + RIGHT(creditCardNumber, 4)
WHERE myDate < '2010-Feb-02'
IF @@ROWCOUNT = 0
BEGIN
COMMIT TRANSACTION
BREAK
END
COMMIT TRANSACTION
-- 1 second delay
WAITFOR DELAY '00:00:01'
END -- WHILE
GO
ROLLBACK
这个问题虽然在数据库中有些记录显然是伪造的信用卡,甚至可能没有4位数。基本上我需要更新这个是添加相同数量的X并保留最后4位数。如果少于4位数,则再添加一些X.
实施例
1242 would turn into XXXXXX1242
12 would turn into XXXXXXXX12
1234567890 would turn into XXXXXX7890
答案 0 :(得分:3)
如果唯一的问题是某些数字少于四个字符,那么左边填充它吗?
请参阅Most efficient T-SQL way to pad a varchar on the left to a certain length?
解释如何
对('XXXXXXXXXXXX'+ @str,@ n)
作品。
答案 1 :(得分:2)
Declare @xx VARCHAR(20)
DECLARE @length INT
SET @length = 17
SET @xx = 'XXXXXXXXXXXXXXXX'
UPDATE TOP ( 10000 ) tblMyTable
SET creditCardNumber = SUBSTRING(@xx, 0, @length - len(RIGHT(creditCardNumber, 4))) + RIGHT(creditCardNumber, 4)
WHERE myDate < '2010-Feb-02'
看看是否解决了这个问题。
答案 2 :(得分:1)
select
stuff(account_no,1,len(account_no)-4,REPLICATE('X', LEN(account_no)-4)),
account_no,
stuff(routing_no,1,len(routing_no)-3,REPLICATE('X', LEN(routing_no)-3)),
routing_no
from dbo.PAYER_BANK_DETAIL