Friday, February 17, 2012

csv matching problem

Dear all,

I have two table, both table have a col which stored the data in CSV format, ie "ab , bc, de", etc

I would like to select the item where "any one item" in first table is same as "any one item" on second table.

I have alread have the private CSV function which can convert the string and return as table. However, I find i cannot do it like this
select .... where dbo.CSVfunction(tableA.colA)

Thx
//CSV function
declare @.separator char(1)
set @.separator = ','

declare @.separator_position int
declare @.array_value varchar(1000)

set @.array = @.array + ','

while patindex('%,%' , @.array) <> 0
begin

select @.separator_position = patindex('%,%' , @.array)
select @.array_value = left(@.array, @.separator_position - 1)
select @.array_value = RTrim(@.array_value)
select @.array_value = LTrim(@.array_value)

Insert @.StringTable
Values (Cast(@.array_value as varchar(50)))

select @.array = stuff(@.array, 1, @.separator_position, '')
end
re @.separator char(1)
set @.separator = ','

declare @.separator_position int
declare @.array_value varchar(1000)

set @.array = @.array + ','

while patindex('%,%' , @.array) <> 0
begin

select @.separator_position = patindex('%,%' , @.array)
select @.array_value = left(@.array, @.separator_position - 1)
select @.array_value = RTrim(@.array_value)
select @.array_value = LTrim(@.array_value)

Insert @.StringTable
Values (Cast(@.array_value as varchar(50)))

select @.array = stuff(@.array, 1, @.separator_position, '')
end

The question is why are you storing data in CVS format in SQL Server when you can store the data as Char, Varchar, Nchar, Nvarchar, Text and Ntext or as Word file using Image. The reason CVS file have to be converted or tranformed with DTS before SQL server can use the file. Hope this helps.

No comments:

Post a Comment