how to get comma separated values in sql | sql server interview questions and answers | sql server

preview_player
Показать описание
#ssunitech #SQLInterviewQuestionsandanswers #sqlInterviewQuestions #sqlInterviewQuestionsForTesting
how to get comma separated values in sql | sql server interview questions and answers | sql server

--Scripts--
Create Table Comma_Separated_Tbl
(
TrainingId Int ,
Training varchar(100),
Classroom varchar(100),
StartTime Time,
duration numeric(14,2),
wk varchar(10)
)

Insert Comma_Separated_Tbl
Select 1,'SQL Server','Silver Room','10:00',2.00,'M'
Union
Select 2,'SQL Server','Silver Room','10:00',2.00,'W'
UNION
Select 3,'SQL Server','Silver Room','10:00',2.00,'T'
Union
Select 4,'SQL Server','Silver Room','10:00',2.00,'F'
UNION
Select 5,'MSBI','GOLD Room','11:00',1.45,'F'
Union
Select 6,'MSBI','GOLD Room','11:00',1.45,'M'
Union
Select 7,'MSBI','GOLD Room','11:00',1.45,'TH'

Select *
from Comma_Separated_Tbl

SELECT DISTINCT Training,Classroom,StartTime,duration,STUFF(
(SELECT ',' + wk FROM Comma_Separated_Tbl
WHERE Training=A.Training FOR XML PATH ('')), 1, 1, ''
)
FROM Comma_Separated_Tbl AS A

For Quiz-

Find Us On FaceBook-
Рекомендации по теме
Комментарии
Автор

select Training, Classroom, StartTime, duration, STRING_AGG(wk, ', ')
from Comma_Separated_Tbl
group by Training, Classroom, StartTime, duration


This is the simplest one

sairamprasadg
Автор

Thanks so much, it's really helpful.

Duration is not giving aggregated values for 3 days. We need to use Sum and group by to solve this!

Hope that's not the idea for this session but thought of sharing.

mariapradeepm
Автор

Tq so much bro, u saved me just now I got dis question 👍👍👍

lokesh
Автор

I think we can use Wk+', ' like this can give comma after the first week in the table...How we can do the reverse of the query comma separate values into new rows. Good approach and nice explanation.

AK-rwzq
Автор

Wonderfull sir Thank you so much for sharing information:)

chandrakumarreddyvennapusa
Автор

Very nice video... waiting for the next...

shubhangiagarwal
Автор

Really appreciate!!
Thanks for sharing..

maheshkumar-vnsn
Автор

Good explanation!
It should get more views and likes..

supervisiongameplay
Автор

Awesome video... It should get more views and likes👍👍

vaibhavsingh
Автор

Sir if the output 0f this tutorial consider as a input and want the output like tutorial query
Than how we solve this problem

ShivamKashyap-iljl
Автор

sir, its a really helpful videos for SQL server Interview preparation . Can we use STRING_AGG function to get this output ?

jyothiprasadkr
Автор

Awesome Video ....it's a request can you upload all important interview questions in one vedio if possible .. I follow and see all your videos and it's very helpful in my SQl related skills

saketshrivastava
Автор

What if i have to find delimiter present in the table ? Can i use xml logic

ambikakheny
Автор

One more thing please post queries regarding to videos on your facebook page or blog for practice purpose.

gagandeepsingh
Автор

It's very common interview questions, I was waiting for it. Thanks for sharing..

ssunitech
Автор

Training clsrm start-time duration wk
SQl. Silver. 10. 3. M
T
W
F

Bro...tell me how to display like this

mahanteshchougala
Автор

How to achieve this same in presto database Sir

esmailpaltasingh
Автор

distinct command is not working at last🤷‍♂

ArshadKhan-hhgf