0

Hi guys can someone help me to write this?

DECLARE @STR VARCHAR(55) = 'Name';
SELECT // SOME LOGIC


RESULT => 'NXXE'

I would like this logic to be generalized to replace every string except the first and last character.

0

2 Answers 2

3

if dynamic data masking is not an answer you can use query below :

DECLARE @str VARCHAR(100) = 'yourstring'
SELECT UPPER(LEFT(@str,1) + REPLICATE('x',LEN(@str) -2)+ RIGHT(@str,1))
Sign up to request clarification or add additional context in comments.

1 Comment

@GiovanniGiampaolo you are welcome , to help community accept it as answer if it helped
-1

This is what you want

DECLARE @STR VARCHAR(55) = 'Name';

declare @i int

Set @i = 0

while @i <= len(@STR )
begin
    select @i = @i + 1
if(@i>1 AND @i<len(@STR ))
    select @STR = STUFF(@STR , @i, 1, 'X')
END
SELECT @STR

1 Comment

Loops are bad in SQL - set-based is the way to go.

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.