====== pears.CommaThousandsDec ======
Generated schema reference. Regenerate this page from the SQL unload; keep hand-maintained business notes in the narrative namespace.
===== Original SQL =====
create function "pears"."CommaThousandsDec"(
/* Application Maintained Function / Procedure - DO NOT EDIT*/
in "d" double )
returns char(40)
begin
declare "rv" char(40);
declare "s" char(20);
declare "t" char(20);
declare "i" integer;
declare "c" char(1);
declare "fract" double;
set "fract" = "abs"("remainder"("d",1));
if "fract" = 0 then set "fract" = null
end if;
if "d" is null then
return null
end if;
if "d" < 0 then
set "d" = "abs"("d");
set "c" = '-'
else
set "c" = ''
end if;
set "rv" = '';
set "s" = "trim"("str"("d",20,0));
set "i" = -1;
"lbl": loop
set "t" = "substr"("s","i",-3);
if "t" = '' then
leave "lbl"
end if;
if "rv" <> '' then
set "rv" = ','+"rv"
end if;
set "rv" = "t"+"rv";
set "i" = "i"-3
end loop "lbl";
return("c"+"rv"+"string"("fract"))
end