how can i replace characters between x and y char in string in SQL SERVER. For example if i have: TEST123456 to have at the end TE******56. I don't know how long will be the string but i know that i need to mask characters between x and y in string
how can i replace characters between x and y char in string in SQL SERVER. For example if i have: TEST123456 to have at the end TE******56. I don't know how long will be the string but i know that i need to mask characters between x and y in string
5条答案
按热度按时间sy5wg1nm1#
You can do it using
REPLICATE()
andSTUFF()
as:While you say "I don't know how long will be the string", the
REPLICATE()
function will return stars "*" accourding to the@Start
and@End
you provide.slsn1g292#
You may try something like this
Output
lf3rwulv3#
Use a combination of
STUFF
,REPLICATE
andCHARINDEX
.CHARINDEX
will give you the position of a particular string inside another one (the first by default).REPLICATE
will repeat a string N amount of times, we use this for the mask.STUFF
will replace a string inside another one (the mask inside your original string) and at the same time remove an N amount of characters at the specified position.kg7wmglp4#
My requirement was different from the post, posting here my solution as it may help someone else.
pokxtpni5#
Perhaps i have misread the question but if you are to store this data you should be able to use Dynamic Data Masking against the target Column?
Then just insert into your table the original Value,
Provided you dont grant the unmask permission to a user the output will look like:
If you grant the Unmask permission to a user and they queory the same data they will see the unmasked result.
https://learn.microsoft.com/en-us/sql/relational-databases/security/dynamic-data-masking?view=sql-server-ver16