Avatar of Jimbo99999
Flag for United States of America asked on

SQL Query - Distinct with Concatenated Columns

Good Day Experts!

I am in need of assistance with a SQL query for use in my VB.net application.  I am getting first name and last name from seperate columns in a table. Then I had to concatenate the two seperated by a comma to show Last, First in my combo box.  

The trouble lies in the SQL table...there are at times 2 records with the same first and last name as a result of DataEntry duplication.  So, I am seeing 2 rows of Smith, Bob in my combobox.  

How can I get only one to be returned back in my query results? I tried looking at Distinct but could not get it to work.  

Here is my query that works but shows duplicates:

Select lname + ',' + fname as name from Login where corporation like '%Contech%'
                                        order by name asc

Microsoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon
Anil Golamari

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

have you tried this

Select distinct(lname + ',' + fname) as name from Login where corporation like '%Contech%'  order by  distinct(lname + ',' + fname)

It has an issue with the order by 1,2 but I was able to get the following version to work.  Surprisingly it was ordered properly.

SELECT     lname + ',' + fname AS name
FROM         (SELECT DISTINCT lname, fname
                       FROM          Login
                       WHERE      corporation LIKE '%Contech%') a

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.