in Education by
Consider a database table holding names, with three rows: Peter Paul Mary Is there an easy way to turn this into a single string of Peter, Paul, Mary? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
In SQL 2005, if you are trying to join two tables with one-to-many relationships use XML PATH method to concatenate the rows: Are you interested in learning SQL from scratch! Have a look at this interesting video on SQL provided by Intellipaat: Table: Employees DepatmentID EmployeeName OP Jashmine OP Sam OP Dalia MN Moly MN Edward Result: DepartmentID EmployeeName OP Jashmine, Sam, Dalia MN Moly, Edward Use this code: SELECT DISTINCT EM2.DepartmentID, SUBSTRING((SELECT ','+EM1.EmployeeName AS [text()] FROM dbo.Empolyees EM1 WHERE EM1.EmployeeID = EM2.EmployeeID ORDER BY EM1.DepartmentID FOR XML PATH ('') ), 2, 1000) [Employees] FROM dbo.Employees EM2 You can also use STRING_AGG to do the same in this way: SELECT STRING_AGG( ISNULL(EmployeeName, ' '), ',') As Countries From FROM dbo.Employees EM

Related questions

0 votes
    I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I didn't see any similar questions asked on this topic, and I had to research this for something I'm working ... had the same question. Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part: 2008-09-22 00: ... How can I get that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table (col1, col2, ... .id = other_table.id Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the ... of the management studio.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any ... in SQL to do this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any ... in SQL to do this? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Just wondering if any of you guys use Count(1) over Count(*) and if there is a noticeable difference in ... SQL Server 2005.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Could anybody tell me where I can learn Microsoft SQL Server? Select the correct answer from above options...
asked Jan 6, 2022 in Education by JackTerrance
0 votes
    How to concatenate multiple rows into a single string in sql a. COALESCE b. STRING_AGG C. Option 1 & 2 d. None of the above...
asked Feb 19, 2023 in Technology by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how long will it take to learn SQL? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...