Solved

How can I mask so many characters of a first name and last name when they are in the same column?

Posted on 2014-10-31
4
92 Views
Last Modified: 2015-04-22
If I have a column that provides a list of names in First Last format,  how can I mask it so that a few of the characters of the first name and so many characters of the last name are masked?  The first and last names are in the same column.

So if I have a name of Bob Jackson, I would like to see something like B** J******?
0
Comment
Question by:Erik Hauser
  • 3
4 Comments
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 125 total points
ID: 40416136
For oracle:

If the first and last name have normal capitalization (i.e. the first letter of each is capital, the rest lower) then try this..

SELECT REGEXP_REPLACE('Bob Jackson', '[a-z]', '*') FROM DUAL;

if that's not the case, but it's legal to treat them that way, try this...

SELECT REGEXP_REPLACE(INITCAP('bob JACKSON'), '[a-z]', '*') FROM DUAL
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 375 total points
ID: 40417294
For sql server, try (not tested) -
with cte as
(select charindex(' ',name) as space, len(name) - charindex(' ',name) as lengthlast from table)
select substring(name,1,1) + replicate('*',space - 2) + substring(name,space + 1,1) + replicate('*',lengthlast - 1) as masked
from cte;
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 375 total points
ID: 40417295
I think I missed concatenating a space between the first and last names, here -
...space - 2) + ' ' + substring...
0
 
LVL 32

Accepted Solution

by:
awking00 earned 375 total points
ID: 40417300
The space could also be maintained using -
select substring(name,1,1) + replicate('*',space - 2) + substring(name,space,2) + replicate('*',lengthlast - 1)
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now