Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Example:
Kumar
Kumar1
Kumar2
123Kumar
2Kumar1
Muruga1
Muruga2
123Muruga
1Muruga3

result:
Kumar,Muruga


How to solve. Don't used foreach condition? pls help this answer.

What I have tried:

[Removed duplicate information already provided in the question body. No SQL command or source code provided.]
Posted
Updated 4-Sep-17 6:35am
v2
Comments
Kornfeld Eliyahu Peter 4-Sep-17 9:44am    
1. How this is related to SQL-Server?
2. The data is always numeric mixed with a name?
3. What have you tried?

Pretty much, you can't: there are no distinct values there - or rather they are all distinct values.
And trying to look for rows which contain other rows is horrible in SQL - the string handling is primitive at best, and you need to remove all non-alpha characters.
Do it in the presentation language, and it's pretty simple (if still nasty looking):
C#
string[] names = { "Kumar", "Kumar1", "Kumar2", "123Kumar", "2Kumar1", "Muruga1", "Muruga2", "123Muruga", "1Muruga3" };
string[] distinct = names.Select(n => new string((n.Where(c => char.IsLetter(c))).ToArray())).Distinct().ToArray();
 
Share this answer
 
Comments
Graeme_Grant 4-Sep-17 10:46am    
5'd! Nice and neat!

[edit] This comes undone if the match contains non-letters... Hmm... not clear if this is an issue or not based on the vagueness of the question....
SQL
CREATE TABLE yourtable
	([ID] int, [User] varchar(10), [Department] varchar(8))
;
	
INSERT INTO yourtable
	([ID], [User], [Department])
VALUES
	(1, 'User1', 'Admin'),
	(2, 'User1', 'Accounts'),
	(3, '1User2', 'Finance'),
	(4, 'User3', 'Sales'),
	(5, '123User3', 'Finance')
;

select distinct REPLACE(REPLACE(REPLACE(t.[user], '1', ''), '2', ''), '3', '')
from yourtable t;

Try it out online on: SQL Fiddle[^]
 
Share this answer
 
v2
Comments
RedDk 4-Sep-17 13:01pm    
This is the right answer.
RickZeeland 4-Sep-17 13:02pm    
You are right :)
itsmypassion 5-Sep-17 2:00am    
It worked
Maybe this:
C#
internal static class Program
{
    private static void Main()
    {
        var list = new List<string>() {
            "Kumar",
            "Kumar1",
            "Kumar2",
            "123Kumar",
            "2Kumar1",
            "Muruga1",
            "Muruga2",
            "123Muruga",
            "1Muruga3"
        };

        foreach (var result in list.Select(x => GetLongestMatch(x, list.Except(new List<string> { x }))).Distinct())
            Console.WriteLine($"{result.Key} was found {result.Value} time{(result.Value > 1 ? "s" : "")}");

        Console.WriteLine("-- done --");
        Console.ReadKey();
    }

    private static KeyValuePair<string, int> GetLongestMatch(string item, IEnumerable<string> list)
    {
        var results = new Dictionary<string, int>();
        foreach (string subStr in GetAllSubstrings(item).OrderByDescending(s => s.Length))
            results.Add(subStr, list.Count(s => s.Contains(subStr)) + 1);

        return results.OrderByDescending(x => x.Value).FirstOrDefault();
    }

    public static IEnumerable<string> GetAllSubstrings(string word)
        => from ndx1 in Enumerable.Range(0, word.Length)
            from ndx2 in Enumerable.Range(0, word.Length - ndx1 + 1)
            where ndx2 >= 2
            select word.Substring(ndx1, ndx2);
}

Outputs this:
Kumar was found 5 times
Muruga was found 4 times
-- done --

Is what you are looking for...
 
Share this answer
 
v4

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