CREATE FUNCTION dbo.GetNextNumberSection(@ANumber INT, @AMinSpace INT)
RETURNS INT
AS
/*
  NB20051025: Created

  Returns a safe section number (used in Identity routines)

  FORMULA EX: (FLOOR((id:123456+space:12345)/spacelength:1000)+1) * spacelength:1000 = newId: 140000
*/
BEGIN
DECLARE @Identity INT, @Position INT, @SpaceLength VARCHAR(20) 
SET @Position = 0
SET @SpaceLength = 1
WHILE @Position < DATALENGTH(CONVERT(VARCHAR, @AMinSpace)) - 1
BEGIN
  SET @SpaceLength = @SpaceLength + '0'  
  SET @Position = @Position + 1
END
--PRINT @ANumber
SET @Identity= (@ANumber + @AMinSpace) / CONVERT(INT, @SpaceLength) + 1
--PRINT @Identity
RETURN @Identity * CONVERT(INT, @SpaceLength) 
 
END

