Technical Article

fnCharPad

,

This function allows the user to pass in a string / character value and it    
will padd the value according to given parameters. The parameters are
as follows:

@ValueToPad = Value to be padded by function.

@PadCharacter = Character used to pad a given value.

@Justification = Justification format bit
0 - Value will be RIGHT justified after padding.
1 - Value will be LEFT justified after padding.

@Width = Total column width of the output after padding.

NOTE - All values passes in for padding must be  of CHAR or
VARCHAR data type.

CREATE FUNCTION [dbo].[fnCharPad] (
@ValueToPad varchar(8000),
@PadCharacter char(1),
@Justification bit,
@Width int
)  
/****************************************************************************************************
This function allows the user to pass in a string / character value and it 
will padd the value according to given parameters. The parameters are 
as follows:

@ValueToPad = Value to be padded by function.

@PadCharacter = Character used to pad a given value.

@Justification = Justification format bit 
0 - Value will be RIGHT justified after padding.
1 - Value will be LEFT justified after padding.

@Width = Total column width of the output after padding.

------------------------------------------------------------------------------------------------
NOTE - All values passes in for padding must be  of CHAR or 
             VARCHAR data type.
*****************************************************************************************************/RETURNS varchar(8000) AS  
BEGIN 

DECLARE @x int

IF @ValueToPad IS NULL SET @ValueToPad = ''
IF @PadCharacter IS NULL SET @PadCharacter = ''

SET @Width = @Width - LEN(@ValueToPad)

--Right Justify Value
IF @Justification = 0
BEGIN
SET @ValueToPad = REPLICATE(@PadCharacter,@Width) + @ValueToPad
END
ELSE
--Left Justify Value
BEGIN
SET @ValueToPad = @ValueToPad + REPLICATE(@PadCharacter,@Width) 
END

RETURN @ValueToPad
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating