DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Optimize Slow Data Queries With Doris JOIN Strategies
  • Data Privacy and Governance in Real-Time Data Streaming
  • Build a Data Analytics Platform With Flask, SQL, and Redis

Trending

  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  1. DZone
  2. Data Engineering
  3. Data
  4. SQL Dynamic Data Masking for Privacy and Compliance

SQL Dynamic Data Masking for Privacy and Compliance

SQL Server Dynamic Data Masking helps protect sensitive data by obscuring it from non-privileged users, ensuring compliance with GDPR, HIPAA, and PCI-DSS.

By 
Dorababu Nadella user avatar
Dorababu Nadella
·
Feb. 10, 25 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.1K Views

Join the DZone community and get the full member experience.

Join For Free

SQL Server Dynamic Data Masking is a feature that allows you to obscure sensitive data from non-privileged accounts, improving data security and compliance. Rather than showing credit card numbers, passwords, or personal identifiers in cleartext, you can define masking rules at the column level so that specific users see only masked values. In contrast, others with elevated permissions see the actual data.

When to Use Dynamic Data Masking

  • Lower environments (development, QA). Typically, developers and testers do not need access to actual sensitive information. Masking ensures that they can work with realistic datasets without risking exposure to PII.
  • Third-party access. When sharing data with external consultants or analytics vendors, masked data prevents inadvertent or malicious disclosure of sensitive content.
  • Regulatory compliance. For environments where regulations like GDPR, HIPAA, or PCI-DSS apply, dynamic masking helps ensure only authorized personnel can view sensitive data in cleartext

Prerequisites

  • SQL server version. Dynamic data masking is available in SQL Server 2016 and later.
  • Permissions and roles. To create or modify masking rules, you must have the ALTER ANY MASK and ALTER permissions on the table. End-users who only have SELECT permissions on the table or view will automatically be served masked data if they do not have UNMASK permission.
  • Assessment of sensitive fields. Identify which columns contain PII or sensitive data. Typical candidates:
    • Email addresses
    • Phone numbers
    • National identifiers (e.g., SSN)
    • Credit card numbers
    • Passwords or security answers

How to Implement Dynamic Data Masking

1. Identify Columns to Mask

 Review each column and decide which requires masking using the query below:

MS SQL
 
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Customers' ORDER BY TABLE_NAME, COLUMN_NAME;


2. Choose a Masking Function

SQL Server provides a few built-in masking functions:

  • Default() – masks the entire value using a default value depending on the data type
  • Email() – masks the email format (e.g., xxx@example.com)
  • Partial (prefix, padding, suffix) – allows partial masking of a string. For example, it shows the first character, masks the middle, and shows the last character.
  • Random ([start range], [end range]) – for numeric columns, a random number in the specified range is returned.

Example Masking Scenarios

  • Name fields (e.g., FirstName, LastName). Use partial() to show maybe the first letter and mask the rest.
  • Email address. Use email() for a consistent masked pattern.
  • Credit card number. Use partial() to show only the last four digits.
  • Password columns. Use default() to mask fully.

3. Apply Masking to the Table

For example, consider a Customers table with columns: FirstName, LastName, Email, CreditCardNumber, and Password. Below are some sample queries:

MS SQL
 
-- Mask the first name to show only the first letter
ALTER TABLE Customers 
ALTER COLUMN FirstName ADD MASKED WITH (FUNCTION = 'partial(1, "****", 0)');

-- Mask the last name similarly
ALTER TABLE Customers 
ALTER COLUMN LastName ADD MASKED WITH (FUNCTION = 'partial(1, "*****", 0)');

-- Mask the email using built-in email masking
ALTER TABLE Customers 
ALTER COLUMN Email ADD MASKED WITH (FUNCTION = 'email()');

-- Mask credit card number to show only the last 4 digits
ALTER TABLE Customers 
ALTER COLUMN CreditCardNumber ADD MASKED WITH (FUNCTION = 'partial(0,"****-****-****-",4)');

-- Mask the password fully
ALTER TABLE Customers 
ALTER COLUMN Password ADD MASKED WITH (FUNCTION = 'default()');


Important: You must be a member of the db_owner role or have the ALTER ANY MASK permission in the database.

4. Create a Non-Privileged User or Role to Test Masking

Use the below queries:

MS SQL
 
CREATE USER MaskedUser WITHOUT LOGIN;
GRANT SELECT ON Customers TO MaskedUser;
When MaskedUser queries the table:

EXECUTE AS USER = 'MaskedUser';
SELECT FirstName, LastName, Email, CreditCardNumber, Password FROM Customers;
REVERT;


They will see masked data. If an administrator with the UNMASK permission runs the same query, they will see the real data.

5. Monitoring and Verification

  • Data audits. Regularly audit queries and logins to ensure no unauthorized UNMASK permissions are granted.
  • Test masking patterns. Confirm that the masked output meets your compliance and business requirements. For instance, ensure the displayed format (like ****-****-****-1234 for a credit card) is correct.
  • Documentation. Maintain a data dictionary or schema documentation that notes which columns are masked and how so that team members understand what they see in downstream environments.

Example Result

Original data (Admins with UNMASK):

FirstName LastName Email CreditCardNumber Password
Alice Nadella alice.n@example.com 4111-1111-1111-1231 MySecretPass%
John Yesu john.y@example.com 5555-6666-7777-8899 Password123$


View for Non-Privileged Users:

FirstName LastName Email CreditCardNumber Password
A**** S***** xxx@xxxx.com ****-****-****-1234 ****
J**** D***** xxx@xxxx.com ****-****-****-8888 ****

 

Conclusion

Implementing Dynamic Data Masking in SQL 

Server is one meaningful step towards what many people have begun to call 'privacy-first' architecture. In any case, the implementation will align with basic data protection principles, such as under GDPR and CCPA, allowing only correctly authorized users to see sensitive data in full view. In contrast, other users get values either masked or partially to decrease the possibility of unauthorized disclosure of personal information.

Data Minimization and Access Control

GDPR and CCPA are based on data minimization, meaning they only provide the data necessary for a particular task. Dynamic masking ensures that you show only the minimal, masked version of sensitive data to non-privileged roles, thus adhering to these regulations.

Improved Protection and Exposure Reduction

It minimizes the risk of personal data exposure by storing sensitive data in masked form within the databases while processing. Should unauthorized access or a data breach at the application/reporting layer occur, the data already shown is masked. Therefore, such an event would have minimal impact on the subjects.

Audit and Compliance Readiness

Well-documented masking rules and role-based permissions support the accountability principle of GDPR and the transparency requirements of CCPA. Auditors can easily verify that your organization has technical measures to safeguard personal information, helping demonstrate compliance and due diligence.

Ease of Implementation in Development and Testing

For lower environments, where developers and testers often need "realistic" data, dynamic masking provides a systematic way to ensure personal information is never exposed. This approach helps maintain privacy protections throughout the data lifecycle.

Scalability and Consistency

Because data masking is dynamic and at the database layer, it scales to multiple applications, services, and analytic tools. This uniformity supports clearly outlined compliance policies and reduces the chance of policy drift or mistakes in bespoke masking logic at different codebases.

Incorporating dynamic data masking into your general privacy-by-design strategy allows you to protect data subjects' privacy interests while lowering the risks of regulatory fines and engendering better customer trust. This fits within the GDPR's focus on privacy by design and by default, and the CCPA demands reasonable security measures to safeguard consumer data.

Data masking Data (computing) Masking (Electronic Health Record) sql

Opinions expressed by DZone contributors are their own.

Related

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Optimize Slow Data Queries With Doris JOIN Strategies
  • Data Privacy and Governance in Real-Time Data Streaming
  • Build a Data Analytics Platform With Flask, SQL, and Redis

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!