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
 SQL Server Development (2000)
 Parsing CSV

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-07-10 : 09:08:16
writes "Hi DB Experts,

Good evening! I have a problem. I have a namelist coming from a distribution list of an active directory. When I converted it to csv file, the members reside in just one column and separated by a comma. I want the names to be separated in a row one by one. I tried it on excel and use the transpose function but to no avail. My last resort is to import it on sql but the names in the column was cut and incomplete. Do you have any idea how to do this. Your help is highly appreciated.

this is the sample file..

names
kelly.yap, lizzy.fox, yahoo, finance.dep, hope.miller, porly.john

the maximum names in a row is 566.

thanks in advance."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-10 : 09:12:19
Use Split function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-10 : 11:21:22
If you need to 'split' for multiple rows, using a numbers/tally table is one approach.

Here's an example...

--data
declare @names table (id int, v varchar(8000))
insert @names
select 1, 'kelly.yap, lizzy.fox, yahoo, finance.dep, hope.miller, porly.john'
union all select 2, 'this, is, another, line, of, text, some, values, have, two words, others have three'
union all select 3, 'this, is, a, third, line'

--calculation
select
id,
ltrim(substring(v, NUMBER, charindex(',', substring(v + ',', NUMBER, 8000))-1)) as name
from dbo.F_TABLE_NUMBER_RANGE(1, 8000), @names
where substring(',' + v, NUMBER, 1) = ','
order by id

/*results
id name
----------- ---------------------------
1 kelly.yap
1 lizzy.fox
1 yahoo
1 finance.dep
1 hope.miller
1 porly.john
2 this
2 is
2 another
2 line
2 of
2 text
2 some
2 values
2 have
2 two words
2 others have three
3 this
3 is
3 a
3 third
3 line
*/


You can find F_TABLE_NUMBER_RANGE here:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685

Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -