Monday, June 20, 2011

Display only last charecters and replace all other digits will be by "X"

There was a request that confidential numbers should be displayed so that only last four digits are displayed and all other digits will be replaced by "X".
I planned to do it in DB and started to search a method to do it. Initially I thought I will need function but after spending some time I found that there is no need to write any function.
Following is the Query that can be used

SELECT  REPLICATE('X',LEN(AccountNo)-4)+RIGHT(RTRIM(AccountNo),4) as AccountNo,AccountNo as s from Accounts