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
 Script Library
 CSV to Multiple Columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 04/27/2006 :  05:55:14  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote

If the table is denormalised and has Comma Seperate Values in a column, this code will copy it to Multiple columns of a Normalised table




declare @DeNormalisedTable table(data varchar(8000))

insert into @DeNormalisedTable 
select '1,Davolio,Nancy' union all
select '2,Fuller,Andrew' union all
select '3,Leverling,Janet' union all
select '4,Peacock,Margaret' union all
select '5,Buchanan,Steven' union all
select '6,Suyama,Michael' union all
select '7,King,Robert' union all
select '8,Callahan,Laura' union all
select '9,Dodsworth,Anne'

select * from @DeNormalisedTable -- Comma Seperated Values

declare @s varchar(8000), @data varchar(8000)
Create table #NormalisedTable (Code int, FirstName varchar(100), LastName varchar(100))

select @s=''

while exists (Select * from @DeNormalisedTable where data>@s)
Begin
	Select @s=min(data) from @DeNormalisedTable where data>@s
	select @data=''''+replace(@s,',',''',''')+''''
	insert into #NormalisedTable	
	exec('select '+@data)
End

select * from #NormalisedTable -- Data in Normalised Table

drop table #NormalisedTable


Madhivanan

Failing to plan is Planning to fail

RyanRandall
Flowing Fount of Yak Knowledge

United Kingdom
1074 Posts

Posted - 04/27/2006 :  06:53:10  Show Profile  Reply with Quote
Nice work Madhivanan - very neat

Here's a link (for others) to the usual alternatives to this sort of problem...
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=CSV,Splitting%20delimited%20lists,Best%20split%20functions

And here's one of those techniques applied to this example (adjusted slightly)...

--data
declare @DeNormalisedTable table(data varchar(8000), Code int, FirstName varchar(100), LastName varchar(100))

insert into @DeNormalisedTable (data)
select '1,Davolio,Nancy' union all
select '2,Fuller,Andrew' union all
select '3,Leverling,Janet' union all
select '4,Peacock,Margaret' union all
select '5,Buchanan,Steven' union all
select '6,Suyama,Michael' union all
select '7,King,Robert' union all
select '8,Callahan,Laura' union all
select '9,Dodsworth,Anne'

--calculation
declare @i int, @j int, @k int
update @DeNormalisedTable set
    @i = charindex(',', data), Code = left(data, @i-1),
    @j = charindex(',', data + ',', @i+1), FirstName = substring(data, @i+1, @j-@i-1),
    @k = charindex(',', data + ',', @j+1), LastName = substring(data, @j+1, @k-@j-1)

select * from @DeNormalisedTable


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

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 04/27/2006 :  09:08:45  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Thanks Ryan. Your method is more simpler than mine

Madhivanan

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

spirit1
Cybernetic Yak Master

Slovenia
11750 Posts

Posted - 04/27/2006 :  10:20:59  Show Profile  Visit spirit1's Homepage  Reply with Quote
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx
point 8



Go with the flow & have fun! Else fight the flow
Blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 04/27/2006 :  11:03:45  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Thanks Mladen. Both of yours and Ryan's are more effecient

Madhivanan

Failing to plan is Planning to fail
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.06 seconds. Powered By: Snitz Forums 2000