Avatar of longinthetooth
longinthetooth
Flag for Canada asked on

Loop through records and split a field

I need to loop through a table where there are 3 columns and split one of columns:

No_ (project No), Code (Category Code), Description, G_L Filter (with filters for  GL Accounts)

No_ and Code combined make the row unique. G_L Filter must be split, looping through the No and Code.

The table has data such as in the attached file:
Example Exp Categories.xlsx

The filter looks like this example: 50000..51000|60000..69999|70000|79999
the  ".." means from and to and the | means or.

I have a split function, but I can only use it when I hardcode the No_ and Code like this:




declare @ParGLList Varchar(8000)		

		set @ParGLList = 
		(select [Filter] from  @table1 
			where [Code] = 'OTHER' and [No_]= 'AV-FND009'
				)


		declare @TempTable Table(Code varchar(200),No_ varchar(20), BeginValue varchar(100),EndValue varchar (100))
		insert into @TempTable (Code, No_, BeginValue, EndValue)
	
		select ' ', ' ', 
				case when replace(SUBSTRING(Value1, 1, CHARINDEX('.', Value1)),'.','')=' ' then Value1 else 
				replace(SUBSTRING(Value1, 1, CHARINDEX('.', Value1)),'.','') end as First_val,
				replace(SUBSTRING(Value1, CHARINDEX('.', Value1) + 1, 100),'.','') Second_val
				 from dbo.Split(@ParGLList, '|')

Open in new window




The result of the script above looks like this:

Code      No_               BeginValue      EndValue
Other      AV-FND009      5215      5217
Other      AV-FND009      5220      5235
Other      AV-FND009      5237      5237
Other      AV-FND009      5240      5240

I would like to loop through the original table and create a new table such as the result script above, without hardcoding each No, Code. That will enable me to compare the values in another table to the "between" BeginValue and EndValue

Help, anyone?
Example-Exp-Categories.xlsx
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
longinthetooth

8/22/2022 - Mon
longinthetooth

ASKER
I managed to loop, but now it seems that I don't need to, because the last loop results in a table with all the results that I need... how do I then not get all the tables?

here is my code

DECLARE @RowsToProcess  int
DECLARE @CurrentRow     int
DECLARE @SelectCol1     varchar(20)
DECLARE @SelectCol2		varchar(20)
DECLARE @SelectCol3		varchar(50)
DECLARE @SelectCol4		varchar(250)


DECLARE @table1 TABLE ( RowID int not null primary key identity(1,1), Code Varchar(20), No_ Varchar(20), [Description]  varchar(50), [Filter] varchar(250))  

INSERT into @table1 (Code, No_, [Description], Filter) 
	SELECT Code, No_, [Description], [G_L Account Filter] FROM [Navigator 5_00_02$Expenditure Category]
SET @RowsToProcess=@@ROWCOUNT
select * from @table1


SET @CurrentRow=0
WHILE @CurrentRow<@RowsToProcess<wbr ></wbr>
BEGIN
    SET @CurrentRow=@CurrentRow+1
    SELECT 
        @SelectCol1=Code,
		@SelectCol2=No_,
		@SelectCol3=[Description],<wbr ></wbr>
		@SelectCol4=[Filter]
        FROM @table1
        WHERE RowID=@CurrentRow 

-- this is the main split process

		declare @ParGLList Varchar(8000)


		set @ParGLList = 
		(select [Filter] from  @table1 
			where @SelectCol1 = Code and @SelectCol2=No_ and RowID=@CurrentRow 
				)


		declare @TempTable Table(Code varchar(20),No_ varchar(20), [Description] varchar(250),BeginValue varchar(100),EndValue varchar (100))
		insert into @TempTable (Code, No_, [Description], BeginValue, EndValue)
	
		
		select	@SelectCol1, @SelectCol2, @SelectCol3, case when replace(SUBSTRING(Value1, 1, CHARINDEX('.', Value1)),'.','')=' ' then Value1 else 
				replace(SUBSTRING(Value1, 1, CHARINDEX('.', Value1)),'.','') end as First_val,
				replace(SUBSTRING(Value1, CHARINDEX('.', Value1) + 1, 100),'.','') Second_val
				 from dbo.Split(@ParGLList, '|')

		select * from @TempTable

--end of split process here

END

Open in new window

ASKER CERTIFIED SOLUTION
longinthetooth

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.
fblack61