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 2000 Forums
 Import/Export (DTS) and Replication (2000)
 delimiting datas in the same column

Author  Topic 

rubishan
Starting Member

2 Posts

Posted - 2005-04-21 : 02:59:50
hi,

i am having a table like
___________________________________
NO | NAME | AGE |
___|____________________|__________|

101 joy 45

102 grace,ruby,leena 22
____________________________________


i want this table to look like as

_______________________________________
NO | NAME | AGE |
___ |____________________|___________ |

101 joy 45

102 grace 22
102 ruby 22
102 leena 22
________________________________________


I s there any sql query to display the table like i mensioned above

I appreciate your immediate response

Thanks



thanks,
Rubitha

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-04-21 : 18:10:32
First, you need to normalize your data so that you don't have this problem anymore.

Second, if the number of values does not exceed 4, then you can use the PARSENAME function. Example to follow. If the number of values exceeds 4, then you can use this:
http://www.sqlteam.com/item.asp?ItemID=2652



CREATE TABLE Table1 (Column1 int, Column2 varchar(50))

INSERT INTO Table1 VALUES(1, 'Tara,Mike')
INSERT INTO Table1 VALUES(2, 'Alex,Rebecca,Zach')
GO

CREATE VIEW View1
AS
SELECT
Column1,
PARSENAME(REPLACE(Column2, ',', '.'), 1) as Part1,
PARSENAME(REPLACE(Column2, ',', '.'), 2) as Part2,
PARSENAME(REPLACE(Column2, ',', '.'), 3) as Part3,
PARSENAME(REPLACE(Column2, ',', '.'), 4) as Part4
FROM Table1
GO

SELECT
Column1,
Part1
FROM View1
WHERE Part1 IS NOT NULL
UNION ALL
SELECT
Column1,
Part2
FROM View1
WHERE Part2 IS NOT NULL
UNION ALL
SELECT
Column1,
Part3
FROM View1
WHERE Part3 IS NOT NULL
UNION ALL
SELECT
Column1,
Part4
FROM View1
WHERE Part4 IS NOT NULL
ORDER BY 1, 2

DROP VIEW View1
DROP TABLE Table1



Tara
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2005-04-22 : 09:29:54
Tara you're a genius, I can use this to solve a problem I was about to tackle


steve

A sarcasm detector, what a great idea.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-04-22 : 12:43:35
Well Damian's the genius: http://www.sqlteam.com/item.asp?ItemID=15044

Tara
Go to Top of Page
   

- Advertisement -