Matt Matt - 1 year ago 46
SQL Question

Hide characters in email address using an SQL query

I have a very specific business requirement to hide certain characters within an email address when returned from SQL and I have hit the limit of my ability with SQL to achieve this. I was wondering if someone out there would be able to point me in the right direction. Essentially, my business is asking for the following:
to become

to become

I am aware that if either portion of the email before of after the @ are less than 3 characters, then this won't work, but I intend on checking for this and dealing with it appropriately. I have tried a mixture of SUBSTRING, STUFF, LEFT/RIGHT etc but I can't quite get it right.

Answer Source

Very interesting and very much tough to generate generic solution try this

this may help you

DECLARE @String VARCHAR(100) = ''

                   (CHARINDEX('.',@String, CHARINDEX('@',@String))-CHARINDEX('@',@String)-3),
                   REPLICATE('*',CHARINDEX('.',@String, CHARINDEX('@',@String))-CHARINDEX('@',@String)))

OUTPUT will be


Similar way for

OUTPUT will be