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)
 Parsing out CSV from text field

Author  Topic 

peryan77
Starting Member

8 Posts

Posted - 2006-02-06 : 10:11:13
I have a big problem. We have a crap un-normalized database (which I take no credit for) and I need to parse out certain values in a text field, the problem is everytime I try to write code to loop through it I get an error because of querying against a text field.

Btw, here is the table setup

Column 1 (varchar 50) Named AccountID
Column 2 (text) Named allowedAccounts

All allowedAccounts are stored as strings ex:

CompanyID AllowedAccounts
========= ================
01 002kkd, 0019393, 00288, 3993k3

I need help writing a function to be able to remove certain accounts. Can anyone help? Here is what I have so far, but it only works if AllowedAccount is varchar, however the len exceeds 8000 in some records.

--Create normalized table to Append Data
create table ReportSubscription (
CompanyID int not null,
NewAllowedAccounts varchar(200) null)

--Need Tally Table
create table Tally (
ID int not null)

go

set nocount on
declare @i int
select @i = 0
while @i <50000
begin
insert into Tally (ID)
select (1 + @i)
select @i = @i + 1
end
go

--Insert Data to Normalized Table
INSERT INTO ReportSubscription SELECT CompanyID,
NullIf(SubString(',' + allowedAccounts + ',' , ID , CharIndex(',' , ',' + allowedAccounts + ',' , ID) - ID) , '') AS Report
FROM Tally, CompanySubscriptionReports
WHERE ID <= Len(',' + allowedAccounts + ',') AND SubString(',' + allowedAccounts + ',' , ID - 1, 1) = ','

--Delete Duplicate Report ID from ReportSubscription
select distinct *
into holding
from ReportSubscription
truncate table ReportSubscription
insert ReportSubscription
select *
from holding
drop table holding

--Delete Reports From 00, 01, 02 and Delete Null
Delete ReportSubscription Where Left(Reports,2) in ('00','01','02')
Delete ReportSubscription where Reports is null
Delete ReportSubscription where Reports = ''

--Truncate CompanySubscriptionReports
truncate table CompanySubscriptionReports

--create a table to work with
create table workingtable (
CompanyID int not null,
NewAllowedAccounts varchar(200) not null,
list varchar(7000),
constraint pk_wt primary key (CompanyID,Reports) )

insert into workingtable (CompanyID ,Reports)
select CompanyID ,NewAllowedAccounts
from ReportSubscription
order by CompanyID ,NewAllowedAccounts

declare
@list varchar(7000),
@lasti int

select
@list = '',
@lasti = -1

--here is the meat of the work
update
workingtable
set
@list = list = case
when @lasti <> CompanyID then NewAllowedAccounts
else @list + ', ' + NewAllowedAccounts
end,
@lasti = CompanyID

--Insert into CompanySubscription
INSERT INTO CompanySubscriptionReports ( CompanyID, allowedAccounts )
select
CompanyID ,
case
when len(max(list)) > 7000 then convert(varchar(7000), left(max(list),6700))
else convert(varchar(7000),max(list))
end as list
from
workingtable
group by
CompanyID
order by
newid()
go

--Remove Unused tables
drop table workingtable
drop table ReportSubscription
drop table Tally

Kristen
Test

22859 Posts

Posted - 2006-02-06 : 19:48:55
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=Best+split+functions

has some stuff on Splitting CSV data stored in a TEXT column, in case that is of help?

Kristen
Go to Top of Page
   

- Advertisement -