Dynamic Data Masking - SQL Server

Dynamic Data Masking (DDM) is a security feature introduced in SQL Server 2016 that allows database administrators to hide sensitive data in query results while the data remains in the database. It is a way to prevent unauthorized access to sensitive data by obscuring it from view in query results.

Here is an example of how to implement DDM in SQL Server:

1) First, let's create a sample table that contains some sensitive data:

CREATE TABLE Customers

(

   CustomerID int PRIMARY KEY,

   FirstName varchar(50),

   LastName varchar(50),

   EmailAddress varchar(100)

);

2) Next, we can use the DDM feature to mask the sensitive data in the "EmailAddress" column. We can use the "email()" function to mask the data with a generic email address:

ALTER TABLE Customers
ALTER COLUMN EmailAddress ADD MASKED WITH (FUNCTION = 'email()');

This will mask the data in the "EmailAddress" column with a generic email address such as "****@*****.com" in query results.

3) We can also use other masking functions to mask different types of data. For example, we can use the "partial()" function to mask the first few digits of a credit card number:

ALTER TABLE Customers
ALTER COLUMN CreditCardNumber ADD MASKED WITH (FUNCTION = 'partial(0,"XXXX-XXXX-XXXX-",4)');

This will mask the first four digits of the "CreditCardNumber" column with "XXXX-XXXX-XXXX-" in query results.

4) Finally, we can query the table to see the masked data:

SELECT CustomerID, FirstName, LastName, EmailAddress, CreditCardNumber FROM Customers;

This will return the data with the sensitive information masked as configured by the DDM feature.

Note that DDM does not affect the actual data in the database. It only masks the data in query results. Therefore, it is not a substitute for proper access control and data encryption.

Comments

Popular posts from this blog

COPILOT Feature in SQL Server 2025

Prefetching - SQL Server

Split Brain - SQL Server