Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » ElevateDB Technical Support » Support Forums » ElevateDB Extensions » View Thread |
Messages 1 to 5 of 5 total |
Functions for LeftJustify(), RightJustify(), CenterJustify() |
Mon, Jul 22 2013 8:14 PM | Permanent 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 AM | Permanent Link |
Roy Lambert NLH Associates 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 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. 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. Tim Young Elevate Software www.elevatesoft.com |
Tue, Jul 23 2013 1:12 PM | Permanent 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"? Barry |
Tue, Jul 23 2013 1:28 PM | Permanent Link |
Roy Lambert NLH Associates Team Elevate | Barry
>I put it in there because if I didn't, you'd be asking "where is it"? I need an image consultant!!!!!! Roy |
This web page was last updated on Friday, April 19, 2024 at 07:09 AM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |