Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Functions for LeftJustify(), RightJustify(), CenterJustify()
Mon, Jul 22 2013 8:14 PMPermanent Link

Barry

Often I need to justify a number or string to the left, right, or center it to a certain width.
Example: Right justify a number with zeroes so it is 5 digits wide, or center fill a string with "-" so it looks like a title.

The function accepts 3 parameters:
1) aValue is the string you wish to fill. If you want to fill a number then you need to use Cast(MyNumber as VarChar) as the parameter.

2) aWidth is the width the result should be

3) aFillChar is an optional character that is used for filling. If excluded, it defaults to a space. If you want to RightJustify a number, use '0'.

Note: If you want to right justify a number like 100, then just use:
rightjustify(cast(100 as VarChar),5,'0')

Barry

CREATE FUNCTION "LeftJustify" (IN "aValue" VARCHAR COLLATE UNI_CI, IN "aWidth" INTEGER, IN "aFillChar" CHAR(1) COLLATE UNI_CI)
RETURNS VARCHAR COLLATE UNI_CI
BEGIN
 Declare Result VarChar default null;
 set Result = Trim(both ' ' from aValue);
 if length(Result) < aWidth then
   set Result = Result + Repeat(IFNULL(aFillChar,' ',aFillChar), aWidth-length(Result));
 end if;
 Return Result;
END

CREATE FUNCTION "RightJustify" (IN "aValue" VARCHAR COLLATE UNI_CI, IN "aWidth" INTEGER, IN "aFillChar" CHAR(1) COLLATE UNI_CI)
RETURNS VARCHAR COLLATE UNI_CI
BEGIN
 Declare Result VarChar default null;
 set Result = Trim(both ' ' from aValue);
 if length(Result) < aWidth then
   set Result = Repeat(IFNULL(aFillChar,' ',aFillChar), aWidth-length(Result)) + Result;
 end if;
 Return Result;
END

CREATE FUNCTION "CenterJustify" (IN "aValue" VARCHAR COLLATE UNI_CI, IN "aWidth" INTEGER, IN "aFillChar" CHAR(1) COLLATE UNI_CI)
RETURNS VARCHAR COLLATE UNI_CI
BEGIN
 Declare Result VarChar default null;
 Declare _Left  VarChar default NULL;
 Declare _Right VarChar default NULL;
 set Result = Trim(both ' ' from aValue);
 if length(Result) < aWidth then
   set _Left  = Repeat(IFNULL(aFillChar,' ',aFillChar), Ceil((aWidth-length(Result))/2));
   set _Right = Repeat(IFNULL(aFillChar,' ',aFillChar), aWidth-Ceil((aWidth-length(Result))/2)-length(Result));
   set Result = _Left + Result + _Right;
 end if;
 Return Result;
END
Tue, Jul 23 2013 3:24 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Barry


Neat, but, since I assume its for use in SELECTs rather than UPDATEs I'm can't see anywhere I'd use the LeftJustify function.

Roy Lambert
Tue, Jul 23 2013 9:44 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

<< Often I need to justify a number or string to the left, right, or center
it to a certain width.
Example: Right justify a number with zeroes so it is 5 digits wide, or
center fill a string with "-" so it looks like a title. >>

Nice, thanks. Smile

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jul 23 2013 1:12 PMPermanent Link

Barry

>>Neat, but, since I assume its for use in SELECTs rather than UPDATEs I'm can't see anywhere I'd use the LeftJustify function.<<

Roy,

I put it in there because if I didn't, you'd be asking "where is it"? Smile

Barry
Tue, Jul 23 2013 1:28 PMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Barry


>I put it in there because if I didn't, you'd be asking "where is it"? Smile

I need an image consultant!!!!!!

Roy
Image