Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 logic to get column name into row???

Author  Topic 

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-06-15 : 10:06:49
Hi,

I have table like :
Name Day1 Day2 Day3 Day4
A Y Y
B Y Y
C Y Y Y

Blank field in Day columns can be replaced with 'N'

Now I want as:

Name VALUES
A Day1,Day2
B Day2,Day4
C Day1,Day3,Day4

Is it possible anyhow??
Actually it is an excel report and I need to updated my tables accordingly.
If I could get the above logic this would be easy for me.

Thanks,
Pushkar

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-15 : 10:21:11
quote:
Originally posted by pushp82

Hi,

I have table like :
Name Day1 Day2 Day3 Day4
A Y Y
B Y Y
C Y Y Y

Blank field in Day columns can be replaced with 'N'

Now I want as:

Name VALUES
A Day1,Day2
B Day2,Day4
C Day1,Day3,Day4

Is it possible anyhow??
Actually it is an excel report and I need to updated my tables accordingly.
If I could get the above logic this would be easy for me.

Thanks,
Pushkar

May be this?
SELECT
[Name],
STUFF(
CASE WHEN Day1 = 'Y' THEN ',Day1' ELSE '' END +
CASE WHEN Day2 = 'Y' THEN ',Day2' ELSE '' END +
CASE WHEN Day3 = 'Y' THEN ',Day3' ELSE '' END +
CASE WHEN Day4 = 'Y' THEN ',Day4' ELSE '' END, 1,1,'')
FROM
YourTable
Go to Top of Page

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-06-18 : 07:52:09
Thanks a lot that worked for me ........ again thank u

quote:
Originally posted by sunitabeck

quote:
Originally posted by pushp82

Hi,

I have table like :
Name Day1 Day2 Day3 Day4
A Y Y
B Y Y
C Y Y Y

Blank field in Day columns can be replaced with 'N'

Now I want as:

Name VALUES
A Day1,Day2
B Day2,Day4
C Day1,Day3,Day4

Is it possible anyhow??
Actually it is an excel report and I need to updated my tables accordingly.
If I could get the above logic this would be easy for me.

Thanks,
Pushkar

May be this?
SELECT
[Name],
STUFF(
CASE WHEN Day1 = 'Y' THEN ',Day1' ELSE '' END +
CASE WHEN Day2 = 'Y' THEN ',Day2' ELSE '' END +
CASE WHEN Day3 = 'Y' THEN ',Day3' ELSE '' END +
CASE WHEN Day4 = 'Y' THEN ',Day4' ELSE '' END, 1,1,'')
FROM
YourTable


Go to Top of Page
   

- Advertisement -