Click here to Skip to main content
16,017,246 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I have a table of the following:

FileKey Title FirstName MiddleName LastName
1 Mr Tim Adikari
2 Ms Sue Andrew
3 Mr Mathew William Anyalai
3 Mrs Elaine June Blazejczyk
4 Mr Scott Alfred Bond
4 Mr Tony Bromwich


I need to produce result of the following based on the filekey column

1-Tim Adikari
2-Sue Andrew
3-Mathew William Anyalai,Elaine June Balzejczyk
4-Scott Alfred Bond,Tony Bromwich


How can I do that? Anyone help

Thanks
Nasif
Posted

Hi,

SQL
select name + '' + lastname from emplyee
 
Share this answer
 
Try This.

SELECT CONVERT(VARCHAR(5),FileKey) + ' - ' + Title + ' ' + FirstName + ' ' + MiddleName + ' ' + LastName
FROM YourTableName
 
Share this answer
 
See this

C#
DECLARE @tbl as table (FileKey nvarchar(10),Title  nvarchar(10),FirstName  nvarchar(10),MiddleName  nvarchar(10),LastName nvarchar(10))
INSERT INTO @tbl 
SELECT '1','Mr','Tim','','Adikari'
UNION
SELECT '2', 'Ms','Sue','','Andrew'
UNION
SELECT '3', 'Mr','Mathew','William','Anyalai'
UNION
SELECT '3', 'Mrs','Elaine','June','Blazejczyk'
UNION
SELECT '4', 'Mr','Scott','Alfred','Bond'
UNION
SELECT '4', 'Mr','Tony','','Bromwich'


SELECT p1.FileKey,
      ( SELECT Title + ' ' + FirstName + ' ' +  MiddleName + ' ' + LastName + ','
          FROM @tbl p2
         WHERE p2.FileKey = p1.FileKey
         ORDER BY FileKey
           FOR XML PATH('') ) AS Products
  FROM @tbl p1
 GROUP BY FileKey ;


Replace
@tbl
in above query with your actual table name.
 
Share this answer
 
with cte
as
(
SELECT * FROM yourtablename
)

SELECT p1.FileKey,
(SELECT Title + '-' + FirstName + ' ' + MiddleName + ' ' + LastName + ','
FROM cte p2
WHERE p2.FileKey = p1.FileKey
ORDER BY FileKey
FOR XML PATH('') ) AS FullName
FROM cte p1
GROUP BY FileKey ;


I have used common table expression
 
Share this answer
 
Hi,

Try this

SQL
SELECT (CAST(FileKey AS VARCHAR)+'-'+STUFF(( SELECT ',' + CONCAT(ISNULL(tc.FirstName+' ',''),
ISNULL(tc.MiddleName+' ',''),ISNULL(tc.LastName,''))
FROM YourTable tC WHERE YourTable.FileKey= tc.FileKey
FOR XML PATH('') ),1,1,'')) DesiredValue  FROM YourTable GROUP BY FileKey


Hope .. this will help.
 
Share this answer
 
v2

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