troubleshooting Question

Loop through records and split a field

Avatar of longinthetooth
longinthetoothFlag for Canada asked on
Microsoft SQL Server 2008
2 Comments1 Solution340 ViewsLast Modified:
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, '|')

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?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros