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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Replace/Insert Values

Author  Topic 

wowachr
Starting Member

2 Posts

Posted - 2012-03-06 : 10:36:53
Hi,

I have a small SQL problem and would really appreciate some help. I import following tables into a database:

Name_____ Hobby
__________Football____Tennis____Baseball____PS3____...
John_________x_______________________________x_____...
Jack_______________________________x_______________...
Abby_________________________________________x_____...

Now I would like to change this table, so I get:

Name______Hobby

John______Football
John______PS3
Jack______Baseball
Abby______PS3
...

Does anybody have an idea how I could do it?

Thanks a lot in advance!

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-06 : 11:34:25
can you post the DDL?

Is football, tennis, baseball, etc all their own columns?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2012-03-06 : 11:36:02
In think UNPIVOT might be what you need here:

[url]http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/[/url]




---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-03-06 : 11:37:36
can you post the table definition for the first table please? I can't tell what it looks like. Or what the data is like

Please provide a script like

CREATE TABLE #hobbies (
[Name] VARCHAR(50)
, <Rest of columns>
)
INSERT #hobbies ....

Then we can help out. You don't need to put much data in there. Just a sample.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-06 : 11:44:51
maybe



CREATE TABLE myTable99
(Name varchar(10), Football char(1), Tennis char(1), Baseball char(1), PS3 char(1))
GO

INSERT INTO myTable99(Name, Football, Tennis, Baseball, PS3)
SELECT 'John','x', null, null, 'x' UNION ALL
SELECT 'Jack',null, null, 'x', null UNION ALL
SELECT 'Abby',null, null, null, 'x'

SELECT * FROM myTable99

SELECT Name, 'Football' AS Hobby
FROM myTable99
WHERE Football IS NOT NULL
UNION ALL
SELECT Name, 'Tennis' AS Hobby
FROM myTable99
WHERE Tennis IS NOT NULL
UNION ALL
SELECT Name, 'Baseball' AS Hobby
FROM myTable99
WHERE Baseball IS NOT NULL
UNION ALL
SELECT Name, 'PS3' AS Hobby
FROM myTable99
WHERE PS3 IS NOT NULL
ORDER BY Name, 2
GO

DROP TABLE myTable99
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -