March 13, 2009 at 11:33 am
I need to change the values in a field, leaving the last 4 characters only. Would like to change the characters to the left of these last 4 to ****. Please advise.
March 13, 2009 at 11:44 am
Hi Robert
DECLARE @phone VARCHAR(100)
SET @phone = '1234567890123456'
SELECT REPLICATE('*', 12) + RIGHT(@phone, LEN(@phone) - 12)
Greets
Flo
March 13, 2009 at 11:57 am
same thing, different way:
[font="Courier New"]UPDATE mytable
SET myfield= '************' + RIGHT(myfield,4)
WHERE LEN(myfield) = 16 --only change items that are exactly 16 chars
SELECT * FROM mytable WHERE LEN(myfield) <> 16 [[/font]
Lowell
March 13, 2009 at 12:02 pm
If you want to keep the field to be the same length as it was, but leave a maximum of 4 chars kept at the end, use
UPDATE tablename
SET fieldname=replicate(‘*’,len(fieldname)-4) + right(fieldname,4)
WHERE LEN(fieldname)>4
March 13, 2009 at 12:28 pm
If you were trying to mask data such as a phone number or a ssn and you still want the data to have the masked appearance of the original value you could use:
DECLARE @ssn VARCHAR(100)
SET @ssn = '123-45-6789'
SELECT rtrim(ltrim(REPLICATE('*', 3) + SUBSTRING(@ssn, 4,1) + REPLICATE('*', 2) + SUBSTRING(@ssn, 7,1)+ RIGHT(@ssn, LEN(@ssn) - 7))) as SSN
Narrio......:)
March 13, 2009 at 10:25 pm
And yet another 😛
declare @number varchar(100)
set @number = '12345678902331234'
select @number as [before], len(@number) as [length]
set @number = STUFF(@number,1,len(@number)-4,REPLICATE('*',len(@number)-4))
select @number as [after], len(@number) as [length]
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply