1

Currently I have a column in my SQL table that saves payment information. For example a column might have "VISA-2435 exp:12/13 Auth#32423". I want to edit the VISA-2435 to display instead VISA-XXXX. Each row occurs is different so I can't do a simple search and replace for that static string.

I tried the following query, but i'm not taking into account how the string might be different

 UPDATE messages 
 SET message = REPLACE(message, LIKE'%Visa-2334%', 'VISA-xxxx')
 WHERE message LIKE '%Visa'

Also, I could changed my mind and just edit the exp:12/13 portion of the string instead.

Does anybody have any suggestions?

4
  • why don't you use substring from the column to get only a number of characters? Commented Jan 1, 2014 at 18:09
  • i'm afraid that if I use a substring I would need to provide a fixed starting position. My strings are not all equal in length. Commented Jan 1, 2014 at 18:15
  • 1
    please share some sample data... you may be able to use regex to do so Commented Jan 1, 2014 at 18:34
  • This is a sample inside the column: "VISA-2435 exp:12/13 Auth#32423" and yes they are each separated by spaces. Commented Jan 1, 2014 at 18:50

3 Answers 3

1

Use a table value function (TVF) or CLR function. I think the TVF might be faster but you will have to test.

Here is a quick implementation of a TVF. It masks all numbers. From a privacy standpoint, that is best!

--
-- Create a table value function
--

CREATE FUNCTION MaskNumbers (@input_txt varchar(128))
RETURNS TABLE
AS
RETURN 
(
 SElECT 
   replace(
   replace(
   replace(
   replace(
   replace(
   replace(
   replace(
   replace(
   replace(
   replace(@input_txt, '0', 'X')
     , '1', 'X')
     , '2', 'X')
     , '3', 'X')
     , '4', 'X')
     , '5', 'X')
     , '6', 'X')
     , '7', 'X')
     , '8', 'X')
     , '9', 'X')
  as masked_txt
);

Sample call using your data.

declare @sample varchar(128) = 'VISA-2435 exp:12/13 Auth#32423'
select * from MaskNumbers(@sample);

enter image description here

Sample call using adventure works credit card table.

use AdventureWorks2012;
go

select top 5 * from [Sales].[CreditCard] cross apply MaskNumbers(CardNumber);
go

enter image description here

If you just want to change the 4 digits after VISA, use CHARINDEX() and STUFF();

-- Raw data
declare @sample varchar(128) = 
    'Random Stuff Before vIsA-2435 ExP:12/13 AuTh#32423 Random Words After';

-- Masked data
select 
    @sample as old_sample,
    case
        when charindex('visa-', @sample) > 0 then
            stuff(@sample, charindex('visa-', @sample) + 5, 4, 'XXXX')
        else @sample
    end as new_sample
go

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The problem that I have with this answer is that the OP doesn't want to replace all numbers, only the numbers of the card, so this answer is not what the OP is looking for at all
0

You can use Stuff() function to do the job:

Sample table and data:

Create table table1 (val varchar(50))
Insert into table1 (val) values ('VISA-2435 exp:12/13 Auth#32423')

Query 1:

--To replace 4 numbers after VISA-
Select stuff(val, 6, 4, 'XXXX') col1
From Table1;

Query 2:

--To replace numbers after VISA- and exp:
Select stuff(stuff(val, 6, 4, 'XXXX'), 15,5,'YY/MM') col1
From Table1

Fiddle demo

Comments

0

This code might be a solution :

 UPDATE messages 
 SET message = STUFF(@S,CHARINDEX('-',messages),(PATINDEX('% exp%',messages)-CHARINDEX('-',messages)),'XXXX')
 WHERE message LIKE '%Visa'

2 Comments

Would be my answer as well. might just change the PATINDEX part to PATINDEX('% exp%', Lower(messages)) to make sure that the correct space is picked up.
Thanks it got ok now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.