Link to home
Start Free TrialLog in
Avatar of David C
David CFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Get column name(s) based on value

Hi Experts, I have a table with practice days for a team. I want to run a query that returns a comma separated string of the days. So for the image below, the required result is "Mondays, Thursdays, Fridays"

User generated image
My current query is;

Select [Team Name],[Captain],
Case 'True'
when Monday then 'Mon'
when Tuesday then 'Tue'
when Wednesday then 'Wed'
when Thursday then 'Thu'
when Friday then 'Fri'
End
FROM Practice
where id=11

Open in new window


But it only returns the first true value. If there is a neater way of doing it, that would be great too.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David C

ASKER

Thank you all. Vitor's answer was the nearest which I had to tweak a little for it to work. Jim's contribution on COALESCE was great. I am now trying to make the query neater using it and Scott's is correct too.