CREATE FUNCTION DBA.ConvertToExtents (@extents VARCHAR(100))
RETURNS INTEGER
AS
BEGIN
-- This function cracks the output from a DBCC PAGE dump
-- of an allocation bitmap. It takes a string in the form
-- "(1:8) - (1:16)" or "(1:8) -" and returns the number
-- of extents represented by the string. Both the examples
-- above equal 1 extent.
DECLARE @extentTotal INT ;
DECLARE @colon INT ;
DECLARE @firstExtent INT ;
DECLARE @secondExtent INT ;
SET @extentTotal = 0 ;
SET @colon = CHARINDEX(':', @extents) ;
-- Check for the single extent case
--
IF (CHARINDEX(':', @extents, @colon + 1) = 0)
SET @extentTotal = 1 ;
ELSE
-- We're in the multi-extent case
--
BEGIN
SET @firstExtent = CONVERT (INT, SUBSTRING(@extents, @colon + 1, CHARINDEX(')', @extents, @colon) - @colon - 1)) ;
SET @colon = CHARINDEX(':', @extents, @colon + 1) ;
SET @secondExtent = CONVERT (INT, SUBSTRING(@extents, @colon + 1, CHARINDEX(')', @extents, @colon) - @colon - 1)) ;
SET @extentTotal = (@secondExtent - @firstExtent) / 8 + 1 ;
END
RETURN @extentTotal ;
END ;