| Author |
Topic |
|
lcpx
Yak Posting Veteran
54 Posts |
Posted - 2005-10-24 : 15:03:37
|
| Hi all,I have a table structure as below:create table t ( code varchar(8), data varchar(2000)); insert into t values ('A', '123,456,789');insert into t values ('B', '012,345');insert into t values ('C', '678,901,234'); commit;A 123,456,789B 012,345C 678,901,234I want the final displat like this:CODE COLUMN_V-------- --------A 123A 456A 789B 012B 345C 678C 901C 234So I created a function to parse the CSV string, the function works.create function fn_ParseCSVString(@CSVString varchar(8000) ,@Delimiter varchar(10))returns @tbl table (s varchar(1000))as/*select * from dbo.fn_ParseCSVString ('qwe,c,rew,c,wer', ',c,')*/begindeclare @i int , @j int select @i = 1 while @i <= len(@CSVString) begin select @j = charindex(@Delimiter, @CSVString, @i) if @j = 0 begin select @j = len(@CSVString) + 1 end insert @tbl select substring(@CSVString, @i, @j - @i) select @i = @j + len(@Delimiter) end returnendBut I can't use it properly, I wrote the below sql query but it doesn't work, very confusing. Can anybody help me??SQL test:select t.code, v.* from t, fn_ParseCSVString(t.data,'-') v;Cheers,Ken |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-10-24 : 16:49:20
|
| Hi Ken,You can't use an expression or sql object (like a column) as an argument to a table function. You can only use a constant or a variable that represents a constant.One way to achieve your desired output is detailed in this article:parsing a csv to multiple rowsalso, look at the comments at the end.http://www.sqlteam.com/item.asp?ItemID=2652Be One with the OptimizerTG |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-10-24 : 18:04:58
|
quote: Originally posted by lcpx Hi all,I have a table structure as below:create table t ( code varchar(8), data varchar(2000)); insert into t values ('A', '123,456,789');insert into t values ('B', '012,345');insert into t values ('C', '678,901,234'); commit;A 123,456,789B 012,345C 678,901,234I want the final displat like this:CODE COLUMN_V-------- --------A 123A 456A 789B 012B 345C 678C 901C 234So I created a function to parse the CSV string, the function works.create function fn_ParseCSVString(@CSVString varchar(8000) ,@Delimiter varchar(10))returns @tbl table (s varchar(1000))as/*select * from dbo.fn_ParseCSVString ('qwe,c,rew,c,wer', ',c,')*/begindeclare @i int , @j int select @i = 1 while @i <= len(@CSVString) begin select @j = charindex(@Delimiter, @CSVString, @i) if @j = 0 begin select @j = len(@CSVString) + 1 end insert @tbl select substring(@CSVString, @i, @j - @i) select @i = @j + len(@Delimiter) end returnendBut I can't use it properly, I wrote the below sql query but it doesn't work, very confusing. Can anybody help me??SQL test:select t.code, v.* from t, fn_ParseCSVString(t.data,'-') v;Cheers,Ken
Why not store your data properly normalized, and then you won't need to worry about such work-arounds? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|