Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
"SELECT DISTINCT Username AS ID, Firstname + ' ' + Surname AS Name FROM Person WHERE Firstname LIKE '%" + searchText + "%' OR Surname LIKE  '%" + searchText + "%' OR Username LIKE  '%" + searchText + "%'";

Im trying to search anything thats is the column Firstname, surname and username but i get duplicated records, can someone tell me where is my problem or better yet help me with a more efficient statement. Thank you
Posted
Updated 5-Oct-13 15:58pm
v2
Comments
Pradeep Shukla 5-Oct-13 22:32pm    
It seems improbable to have duplicate records out of this query, you are already using DISTINCT keyword to filter duplicates.. provide example of data ...
Azee 6-Oct-13 6:21am    
Hey there, have you checked your Table data, may be there are duplicate records, e.g, for Firstname and Surname,
check and let me know, if there are, I'll let you know how to avoid the duplicates

Hi there,

First thing is first. Use SQL parameters in your query.
C#
const string connectionStringName = "MyConnectionString";
            var searchText = "something";

            // Add your connection string to the config file. Then you can extract it like this
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionStringName].ToString()))
            {
                using (var command = connection.CreateCommand())
                {
                    // Use a stored procedure, if you can. That's better for performance
                    command.CommandType = CommandType.Text;
                    command.CommandText = "SELECT U.UserName as 'ID', U.FirstName + ' ' + U.LastName as 'Name' from [Users] U WHERE U.UserName LIKE @SearchText OR U.FirstName LIKE @SearchText OR U.LastName LIKE @SearchText";

                    var searchParameter = new SqlParameter("SearchText", searchText);
                    command.Parameters.Add(searchParameter);

                    connection.Open();
                    var reader = command.ExecuteReader();
                }
            }

Use Google or numerous articles on CodeProject to learn anything you don't understand in the above code block.

Now regarding your question. I've created a temp table and run your query like the way you have asked. It does NOT return any duplicates. Perhaps, you might have a different idea of what is a 'duplicate' or you might be trying to achieve something else. Please, explain what this SQL should return data-wise. Are you expecting just one row satisfying the search conditions?
 
Share this answer
 
The only possiblity of getting duplicate rows can be multiple persons having same first name and surname. This would be a practical scenario.
 
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