Question: SQL query to get Column Values with comma(,) Separation
Answer Part-1:
Dear Reader, to see the answer, please watch video by clicking below Link,
ANSWER PART-1
ANSWER PART-2
Practice Query is:
-- table creation
create table TestQ4_1
(
ID int identity(1,1),
EmpName nvarchar(200)
)
Go
-- insert data
Insert into TestQ4_1 (EmpName) Values ('Emp-1'),('Emp-2'),('Emp-3'),('Emp-4'),('Emp-5')
Go
-- without using STUFF
select ','+EmpName from TestQ4_1 for xml path('')
-- With STUFF Function
select STUFF((select ','+EmpName from TestQ4_1 for xml path('')),1,1,'') as AllEmployees
select ID,(select STUFF((select ','+EmpName from TestQ4_1 for xml path('')),1,1,'')) as AllEmployees from TestQ4_1
order by ID
Feel free to mail me for any query or feedback.
Happy Coding.
Rudra Pratap Singh
Software Engineer
singhrudrapratap29@gmail.com
+91-9307474998 (whatsapp)
Answer Part-1:
Dear Reader, to see the answer, please watch video by clicking below Link,
ANSWER PART-1
ANSWER PART-2
Practice Query is:
-- table creation
create table TestQ4_1
(
ID int identity(1,1),
EmpName nvarchar(200)
)
Go
-- insert data
Insert into TestQ4_1 (EmpName) Values ('Emp-1'),('Emp-2'),('Emp-3'),('Emp-4'),('Emp-5')
Go
-- without using STUFF
select ','+EmpName from TestQ4_1 for xml path('')
-- With STUFF Function
select STUFF((select ','+EmpName from TestQ4_1 for xml path('')),1,1,'') as AllEmployees
select ID,(select STUFF((select ','+EmpName from TestQ4_1 for xml path('')),1,1,'')) as AllEmployees from TestQ4_1
order by ID
Feel free to mail me for any query or feedback.
Happy Coding.
Rudra Pratap Singh
Software Engineer
singhrudrapratap29@gmail.com
+91-9307474998 (whatsapp)
No comments:
Post a Comment