问题
I want to change 2 letters and mask the remaining letters. I changed the letters but didn't mask other letter. This change command is
SELECT NAME,
CONCAT(SUBSTRING(NAME, 1, 2),
SUBSTRING(NAME, 4, 1),
SUBSTRING(NAME, 3, 1),
SUBSTRING(NAME, 5, ABS(LEN(NAME) -4)))
AS CHANGELETTER
FROM TESTBILGILER
How can I do masking SQL SERVER 2014?
回答1:
A little ugly, but here is another option
Example
Declare @YourTable table(SomeCol varchar(50))
Insert Into @YourTable values
('Chirstina')
,('John')
,('Susan')
,('Wil') -- May want to wrap in a case for smaller strings
,('Bo') -- May want to wrap in a case for smaller strings
Select Masked = replicate('X',((len(SomeCol)-2)/2))
+substring(SomeCol,((len(SomeCol)-2)/2)+1,2)
+replicate('X',len(SomeCol)-((len(SomeCol)-2)/2)-2)
From @YourTable
Returns
Masked
XXXrsXXXX
XohX
XusXX
WiX
Bo
回答2:
Good day,
In this article you can read about the Database Engine Dynamic Data Masking feature in SQL Server 2016, and in this article you can watch how we can implement the same solution in older versions. The basic idea is to use Views and Triggers in order to create a security layer in the database level.
** if you will post queries to create a specific table and o insert some sample data + the requested result according to the sample data, then we can help you with finding the specific query that you can use in the masking procedure (in the view that implement the masking)
回答3:
This function does what you ask. How does it work? Start with two X's, append the 4th character, append the 3rd character, then append as many X's as there are characters in the original string. This will add 4 more X's than are necessary, but that's okay. Finally, truncate the result to match the length of the original string. This is necessary to deal with input strings that are shorter than 4 characters.
create table X ( Name nvarchar(1000) )
insert into X ( Name ) values
('Christina'), -- 'XXirXXXXX'
('John'), -- 'XXnh'
('Susan'), -- 'XXasX'
('Wil'), -- 'XXl'
('Bo'), -- 'XX'
('Q'), -- 'X'
('') -- ''
select Name, substring ( 'XX' + substring(Name,4,1) + substring(Name,3,1) + replicate('X',len(Name) ), 1, len(Name) ) from X
来源:https://stackoverflow.com/questions/51695230/how-to-sql-server-command