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 setupColumn 1 (varchar 50) Named AccountIDColumn 2 (text) Named allowedAccountsAll allowedAccounts are stored as strings ex: CompanyID AllowedAccounts========= ================01 002kkd, 0019393, 00288, 3993k3I 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 Datacreate table ReportSubscription ( CompanyID int not null, NewAllowedAccounts varchar(200) null)--Need Tally Tablecreate table Tally ( ID int not null) goset nocount ondeclare @i intselect @i = 0while @i <50000begin insert into Tally (ID) select (1 + @i) select @i = @i + 1endgo--Insert Data to Normalized TableINSERT 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 ReportSubscriptionselect distinct *into holdingfrom ReportSubscriptiontruncate table ReportSubscriptioninsert ReportSubscriptionselect *from holdingdrop table holding--Delete Reports From 00, 01, 02 and Delete NullDelete ReportSubscription Where Left(Reports,2) in ('00','01','02')Delete ReportSubscription where Reports is nullDelete ReportSubscription where Reports = ''--Truncate CompanySubscriptionReportstruncate table CompanySubscriptionReports--create a table to work withcreate 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 ,NewAllowedAccountsfrom ReportSubscriptionorder by CompanyID ,NewAllowedAccountsdeclare @list varchar(7000), @lasti intselect @list = '', @lasti = -1--here is the meat of the workupdate workingtableset @list = list = case when @lasti <> CompanyID then NewAllowedAccounts else @list + ', ' + NewAllowedAccounts end, @lasti = CompanyID --Insert into CompanySubscriptionINSERT 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 listfrom workingtablegroup by CompanyID order by newid()go--Remove Unused tablesdrop table workingtabledrop table ReportSubscriptiondrop table Tally