Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 December SQL Server: Concatenate Strings using the GROUP BY clause

SQL Server: Concatenate Strings using the GROUP BY clause

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a script to perform concatenation of strings by applying GROUP BY in SQL Server.

Yesterday, I was preparing one of the reports for the Production server and required the merging of strings basis on GROUP BY columns.
I used only FOR XML PATH to get this solution.

First, create the sample table and data:

1
2
3
4
5
6
7
CREATE TABLE tbl_GroupStringTable ([EmpID] INT, [EmpName] VARCHAR(250), [DeptName] VARCHAR(250))
 
INSERT INTO tbl_GroupStringTable ([EmpID],[EmpName],[DeptName]) VALUES (1,'Anvesh','Sales')
INSERT INTO tbl_GroupStringTable ([EmpID],[EmpName],[DeptName]) VALUES (1,'Neevan','Production')
INSERT INTO tbl_GroupStringTable ([EmpID],[EmpName],[DeptName]) VALUES (2,'Akash','Order')
INSERT INTO tbl_GroupStringTable ([EmpID],[EmpName],[DeptName]) VALUES (3,'Roy','Sales')
INSERT INTO tbl_GroupStringTable ([EmpID],[EmpName],[DeptName]) VALUES (3,'Priyaj','Order')

Execute this script:

1
2
3
4
5
6
7
8
9
10
SELECT
[EmpID],
STUFF((
SELECT ', ' + [EmpName] + ':' + CAST([DeptName] AS VARCHAR(MAX))
FROM tbl_GroupStringTable
WHERE (EmpID = Results.EmpID)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS NameValues
FROM tbl_GroupStringTable Results
GROUP BY EmpID

The Result:

Group String SQL Server

Dec 26, 2015Anvesh Patel
SQL Server:Function to Split String Value Using Different DelimitersSQL Server: Find MAX value from Multiple Columns
Comments: 8
  1. lena
    January 19, 2016 at 8:51 am

    Thanks very nice blog!

  2. jorg
    January 28, 2016 at 5:47 am

    Hello, just wanted to tell you, I liked this article.

    It was practical. Keep on posting!

  3. Roy
    January 29, 2016 at 6:52 pm

    Pretty! This was an incredibly wonderful post.
    Many thanks for supplying these details.

  4. Leo
    February 29, 2016 at 3:36 pm

    I appreciate, cause I found just what I was looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  5. Sandeep
    September 13, 2016 at 9:56 am

    Thanks, It works for us

  6. John
    December 16, 2016 at 8:44 am

    Great post Sir!

    Was struggling grasping this xml path stuff, but that example table did it for me.

    Took me 5 minutes to get my queries right after trying it out using your example data first!

  7. Andrew
    August 30, 2018 at 12:22 pm

    Thank you very much! It was of great help to me.

  8. AJ
    September 20, 2018 at 3:16 pm

    Looks pretty identical to a similar stack overflow question that predates this…

Anvesh Patel
Anvesh Patel

Database Engineer

December 26, 2015 SQL ServerAnvesh Patel, database, database research and development, dbrnd, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....