Usage:
SELECT dbo.PadValue('TEST', 5, '*', 0)
Returns: '*TEST'
Find a String in a String in SQL Server
Learn how you can find data in a string and extract it, no matter where it's located.
2016-03-10
36,994 reads
Usage:
SELECT dbo.PadValue('TEST', 5, '*', 0)
Returns: '*TEST'
/********************************************** Title: fn_PadValue Programer: Mike DiRenzo Date: 1/6/2005 Called By: Params: @inVal varchar(50) - The original value to be padded @PadLimit int - The limit of padding to apply For example, if the limit is 5 and the original length of the value is 3, then the applied padding will be 2 chars. @PadChar char(1) The padding char. It could be anything. Default is '0' @PadSide int The side in which the padding is tobe applied. For example, 0 is Left, 1, is Right, default is Left Returns: Padded version of original value Usage: SELECT dbo.PadValue('TEST', 5, '*', 0) Returns: '*TEST' **********************************************/ Create FUNCTION dbo.fn_PadValue ( @inVal varchar(50), @PadLimit int, @PadChar char(1), @PadSide int ) RETURNS varchar(50) AS BEGIN Declare @retVal varchar(50) Declare @Padding int --Determine the amount of padding Set @Padding = @PadLimit - Len(@inVal) IF @Padding < 0 Set @Padding = 0 IF @PadSide = 0 -- Pad the left side Set @retVal = Replicate(@PadChar, @Padding) + @inVal IF @PadSide = 1 --Pad the right side Set @retVal = @inVal + Replicate(@PadChar, @Padding) RETURN (@retVal) END