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
 Transact-SQL (2000)
 Delimited string

Author  Topic 

makimark
Starting Member

34 Posts

Posted - 2004-09-09 : 03:33:43
Hi
I have a string that looks like this

Field1=abcdef|Field2=12345678888|Field3=abcccdde|Field4=abc

I can do this in DTS but i wondered if it would be possible to pull the string into a single column using T-SQL.Using charindex maybe? The challenge here it that the length of the string after the = sign can vary.

thanks

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-09 : 05:41:02
this should help:


declare @myTable table (myCol varchar(200), Col1 varchar(100), Col2 varchar(100),
col3 varchar(100), col4 varchar(100))

Insert Into @myTable Values ('Field1=abcdef|Field2=12345678888|Field3=abcccdde|Field4=abc', Null,Null,Null,Null)
Insert Into @myTable Values ('Field1=abcde453f|Field2=1234567herhe8888|Field3=abc43z4erccdde9|Field4=ahdrhdbc9', Null,Null,Null,Null)
Insert Into @myTable Values ('Field1=abcdeegreerhgef|Field2=12345hreher6788889|Field3=abcccdffde9|Field4=abrdfdc9', Null,Null,Null,Null)

declare @i1 int
declare @i2 int
declare @i3 int
declare @i4 int

update @myTable set
@i1 = charindex('=', myCol)+1,
col1 = substring(myCol, @i1, charindex('|', myCol)-@i1),
@i2 = charindex('=', myCol, @i1)+1,
col2 = substring(myCol, @i2, charindex('|', myCol,@i2)-@i2),
@i3 = charindex('=',myCol, @i2)+1,
col3 = substring(myCol, @i3, charindex('|', myCol,@i3)-@i3),
@i4 = charindex('=',myCol, @i3)+1,
col4 = substring(myCol, @i4, 255)
select * from @myTable


Go with the flow & have fun! Else fight the flow
Go to Top of Page

makimark
Starting Member

34 Posts

Posted - 2004-09-09 : 06:23:56
Wow, pretty amazing, i was fiddling with loops etc etc. Fiddled myself to a standstill.Thanks a lot.:)
Go to Top of Page
   

- Advertisement -