[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

replace characters other than last four

Hello, I have a standard credit card number that I would like to  do a replace or something to put * for all characters except the last four.

So this:
1234567890123456 or 1234-5678-9123-4567 would become ************3456 or ************4567 respectively

How is that accomplished

UPDATE LMVR_GuestCard
SET CardNumber = ?????
WHERE GuestID = @GuestID
0
gogetsome
Asked:
gogetsome
  • 3
  • 2
  • 2
  • +2
1 Solution
 
matrix_aashCommented:
update yourtable
  set yourfield = replace(yourfield, '4567', '3456' )
where yourfield like 'criteria'
0
 
hieloCommented:
try:
UPDATE LMVR_GuestCard
SET CardNumber = Left(CardNumber,LEN(CardNumber)-4) + '****'
WHERE GuestID = @GuestID
0
 
BrandonGalderisiCommented:
Something like this:

UPDATE LMVR_GuestCard
SET CardNumber = replicate('*, len(cardnumber)-4) + right(cardnumber,4)
WHERE GuestID = @GuestID

It accounts for the difference in length of various credit card types (amex, discover, visa/mc).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gogetsomeAuthor Commented:
I was hoping to not have to actually know the creditcard number. Isnt there some character replace method that uses some sort of ltrim??

UPDATE LMVR_GuestCard
SET CardNumber = ?
FROM LMVR_GuestCard GC, LMVR_PropertyReservations PR
WHERE GC.GuestID = @GuestID
AND
PR.@ReservationId = @ReservationId
AND
GC.GuestID = PR.GuestID
0
 
hieloCommented:
>>I was hoping to not have to actually know the creditcard number.
You don't. I misread you question and put **** only on the last 4 characters, but the suggestion by BrandonGalderisi puts * on all EXCEPT the last four, regardless of the number.
0
 
gogetsomeAuthor Commented:
Exactly what I was after. Thank you!
0
 
BrandonGalderisiCommented:
Thanks for the support while I was AFK hielo!
0
 
hieloCommented:
>>Thanks for the support while I was AFK hielo!
Anytime.

BTW: Good job.
0
 
NagarajuGaragaCommented:
declare @cardNum nvarchar(25)
 set @cardNum ='1234-5678-9123-4567'
 select stuff(@cardNum,1,(len(@cardNum)-4),replicate('*',len(@cardNum)-4))
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now