Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
There is email address in my datablse table column as username
I have to select it from database in such a way so that first two letter and last
two letters only visible before @
for example this email id (sontosh_kumar_rch@yahoo.com) should be selected in below format..


sa...............ch@yahoo.com
Posted
Updated 28-Nov-14 21:43pm
v2

Try:
SQL
SELECT SUBSTRING(userEmail, 1, 2) + REPLICATE('.', CHARINDEX('@', userEmail) - 5)+ SUBSTRING(userEmail, CHARINDEX('@', userEmail) - 2, 9999) AS [User Email] FROM MyTable

Do note that that doesn't do any error checking...
 
Share this answer
 
You can start something like this:

Look at these concepts in SQL:

SUBSTRING[^]
REPLCATE[^]
CHARINDEX[^]

SQL
DECLARE @emailAddress NVARCHAR(100) = 'HelloWorld@gmail.com'

DECLARE @Prefix NVARCHAR(100)
DECLARE @DomainName NVARCHAR(100)

SET @Prefix = LEFT(@emailAddress, CHARINDEX('@', @emailAddress) - 1)
SET @DomainName = RIGHT(@emailAddress, CHARINDEX('@', @emailAddress) - 2)

--Assuming that prefix of email address is always 5 more chatacters
DECLARE @PrefixStart NVARCHAR(100)
DECLARE @PrefixEnd NVARCHAR(100)

SELECT @PrefixStart = SUBSTRING (@Prefix, 1 , 2)
SELECT @PrefixEnd = SUBSTRING (@Prefix, LEN(@Prefix) - 1 , LEN(@Prefix))

SELECT @PrefixStart + REPLICATE('*', LEN(SUBSTRING (@Prefix, 3 , LEN(@Prefix) - 3))) + @PrefixEnd + '@' + @DomainName
 
Share this answer
 
Do it at the code behind, not in sql. Example in c#:
C#
using System;

public class Program
{
    public static void Main()
    {
        string email_id = "sontosh_kumar_rch@yahoo.com";

        int pos = email_id.IndexOf("@");

        if (email_id.IndexOf("@") != -1)
        {
            string left_part = email_id.Substring(0, pos);
            Console.WriteLine(left_part); // sontosh_kumar_rch

            string first_two = left_part.Substring(0, 2);
            Console.WriteLine(first_two);  // so

            string last_two = left_part.Substring(pos - 2);
            Console.WriteLine(last_two); // ch

            string right_part = email_id.Substring(pos);
            Console.WriteLine(right_part); // @yahoo.com

            string result = first_two + new String('.', pos - 4) + last_two + right_part;
            Console.WriteLine(result); // so.............ch@yahoo.com
        }
    }
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900