Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to find unused number in table
Message
From
08/05/2017 04:47:48
 
 
To
08/05/2017 04:26:51
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 10
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01650911
Message ID:
01650928
Views:
57
>>If these barcodes are to be used for commercial trade, you can't just pick any number since the number also contains a control digit.
>
>So he can omit that digit, which then complicates the whole thing - there'd have to be a version of each code, one digit shorter, which would then be accounted for.
>
>>Other than that, this code will find "holes" in a series
>>
>>
Select k.barcode+1 as missing From yourtable k;
>>   Where k.barcode+1 Not in (Select barcode From yourtable)
>
>It will find the beginning of each hole, however it won't find the size of the hole. IOW, if 20 consecutive numbers are missing, it will find the first one. But then with a fox cursor, index and Set Near ON it shouldn't be a problem to find the next existing number and gauge the size of the hole.

This finds the gaps - adapted from the internet a couple of years ago
*_______________________________________________________________________________
function GapsTest()

	create cursor barcode ;
	(	Id	I ;
	)
	
	insert into barcode values (1)
	insert into barcode values (2)
	insert into barcode values (3)
	
	insert into barcode values (25)
	
	insert into barcode values (37)
	
	=GapsFind('Barcode', 'Id')
	
	select GetGaps_MissingByRange 
	browse nowait
endfunc
*_______________________________________________________________________________
function GapsFind(cursorname, fieldName)	

	select NoNextId.&fieldName + 1	as IdFrom, ;
			min(T2.&fieldName) - 1	as IdTo ;
		from ;
		 	( select &fieldName;
				from (m.cursorname) X;
				where	( not exists ;
							(	select *  ;
									from ( ;
											select &fieldName - 1 as IdPrevious ;
												from (m.cursorname) ;
										) IdPrevious ;
										where ( X .&fieldName= IdPrevious.IdPrevious ) ;
							) ;
						) ;
					and	( &fieldName<> ( select max(&fieldName) from (m.cursorname) ) ) ;
			) NoNextId ;
				join  (m.cursorname)  T2 ;
					on	( NoNextId.&fieldName < T2.&fieldName) ;
			group by 1 ;
		into cursor GetGaps_MissingByRange nofilter ;
		order by 1
endfunc
*_______________________________________________________________________________		
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform