SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 logic to get column name into row???
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pushp82
Yak Posting Veteran

67 Posts

Posted - 06/15/2012 :  10:06:49  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/15/2012 :  10:21:11  Show Profile  Reply with Quote
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

67 Posts

Posted - 06/18/2012 :  07:52:09  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000