Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Selective masking in SQL server 2005

4.00/5 (1 vote)
5 Jan 2012CPOL 12.7K  
Masking selected characters in SQL, hiding few characters
SQL
--Mask all but show only last 3 digits of the phone number
DECLARE @PhoneNo varchar(10), @Mask varchar(10)
SELECT @PhoneNo = '1234567890', @Mask = 'XXXXXXXXXX' 

SELECT @PhoneNo as 'Phone No', replace(@PhoneNo, substring(@PhoneNo, 1, len(@PhoneNo) - 3), 
substring(@Mask, 1, len(@PhoneNo) - 3)) as 'MaskedPhoneNumber'

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)