dbo.PartitionLeft, dbo.PartitionRight and dbo.Partition
IF
EXISTS( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[PartitionLeft]') ANDxtype
in (N'FN', N'IF', N'TF') )BEGIN
DROP FUNCTION [dbo].[PartitionLeft]END
GO
CREATE
FUNCTION [dbo].[PartitionLeft](
@value
bigint,@min
bigint,@max
bigint,@range
bigint)
RETURNS
bigintAS
BEGIN
DECLARE @left bigint IF @value < @min OR @value > @max OR @max < @min OR ((@max - @min) + 1) < @range BEGIN SET @left = NULL END ELSE BEGIN SET @left = @min + (CAST((@value - @min) / @range AS bigint) * @range) END RETURN @leftEND
GO
SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[PartitionRight]') AND
IF EXISTS(xtype
in (N'FN', N'IF', N'TF'))BEGIN
DROP FUNCTION [dbo].[PartitionRight]END
GO
CREATE
FUNCTION [dbo].[PartitionRight](
@value
bigint,@min
bigint,@max
bigint,@range
bigint)
RETURNS
bigintAS
BEGIN
DECLARE @right bigint IF @value < @min OR @value > @max OR @max < @min OR ((@max - @min) + 1) < @range BEGIN SET @right = NULL END ELSE BEGIN SET @right = dbo.PartitionLeft(@value, @min, @max, @range) + @range - 1 IF @right > @max BEGIN SET @right = @max END END RETURN @rightEND
GO
IF
EXISTS( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Partition]') ANDxtype
in (N'FN', N'IF', N'TF') )BEGIN
DROP FUNCTION [dbo].[Partition]END
GO
CREATE
FUNCTION [dbo].[Partition](
@value
bigint,@min
bigint,@max
bigint,@range
bigint)
RETURNS varchar
(255)AS
BEGIN
RETURN CAST(dbo.PartitionLeft(@value, @min, @max, @range) AS varchar) + ' - ' + CAST(dbo.PartitionRight(@value, @min, @max, @range) AS varchar)END
GO