Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 query help please

Author  Topic 

PGG_CA
Starting Member

24 Posts

Posted - 2012-04-25 : 16:43:10
I have the following table.

id username name replacement
1 userX MrX 0
2 userY MrY null
3 userA MrA 1
4 userB MsB 3

The replacement is an int field and can contain null. What I want to do is return me a concatenated username strings if the user has a replacement. For example, my query should return like this:

userX
userY
userA;userX
userB;userA

Any help would be appreciated. Thanks.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-04-25 : 17:00:51
SELECT A.UserName+COALESCE(';'+B.UserName,'')
FROM myTable A
LEFT JOIN myTable B ON A.Replacement=B.ID
Go to Top of Page
   

- Advertisement -