I need to do an update on a table that has over 1 million records to mask the credit cards.
I have to keep the last 4 digits and the rest should become X's.
I come up with this so far
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
The problem with this though is there are records in the database that are obviously fake credit cards and may not even have 4 digits. Basically what I need to update this to is this add the same number of X's and keep the last 4 digits. If there are less than 4 digits then add some more X's.
Example
1242 would turn into XXXXXX1242
12 would turn into XXXXXXXX12
1234567890 would turn into XXXXXX7890