dbo.PartitionLeft, dbo.PartitionRight and dbo.Partition

IF EXISTS(

SELECT *

FROM dbo.sysobjects

WHERE id = OBJECT_ID(N'[dbo].[PartitionLeft]') AND

xtype 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 bigint

AS

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 @left

END

GO
IF EXISTS(

SELECT *

FROM dbo.sysobjects

WHERE id = OBJECT_ID(N'[dbo].[PartitionRight]') AND

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 bigint

AS

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 @right

END

GO

IF EXISTS(

SELECT *

FROM dbo.sysobjects

WHERE id = OBJECT_ID(N'[dbo].[Partition]') AND

xtype 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

 

No Comments