Tuesday, October 2, 2018

SQL query to get Column Values with comma(,) Separation : Part 2 - SQL Interview Question

Question: SQL query to get Column Values with comma(,) Separation
Answer Part-2:
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
-- using coalesce (If don't want use XML PATH and STUFF)
declare @tt nvarchar(200)
select @tt=coalesce(@tt+',','')+CAST(EmpName AS nvarchar)
from TestQ4_1
select @tt

Feel free to mail me for any query or feedback.
Happy Coding.

Rudra Pratap Singh
Software Engineer
singhrudrapratap29@gmail.com
+91-9307474998 (whatsapp)

SQL query to get Column Values with comma(,) Separation : Part 1 - SQL Interview Question

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)

How to Get Organic Views on Your Channel/Content

 Hi Friends, This post will be very short and specific in the information.  If you are a content creator and want some help in getting organ...