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)
 What's wrong with the SQL function?

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,789
B 012,345
C 678,901,234



I want the final displat like this:

CODE COLUMN_V
-------- --------
A 123
A 456
A 789
B 012
B 345
C 678
C 901
C 234

So 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,')
*/
begin
declare @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
return
end

But 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 rows
also, look at the comments at the end.
http://www.sqlteam.com/item.asp?ItemID=2652

Be One with the Optimizer
TG
Go to Top of Page

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,789
B 012,345
C 678,901,234



I want the final displat like this:

CODE COLUMN_V
-------- --------
A 123
A 456
A 789
B 012
B 345
C 678
C 901
C 234

So 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,')
*/
begin
declare @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
return
end

But 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?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-25 : 01:40:52
Duplicate post
http://sqlteam.com/forums/topic.asp?TOPIC_ID=56743

Madhivanan

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

- Advertisement -