<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: How do I format numbers in SQLA in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833386#M4864229</link>
    <description>&lt;P&gt;Just a function you might like to use. It's not exactly what you are looking for (and doesn't add leading zeroes) but could help as a starting point.&lt;/P&gt;

&lt;P&gt;I use this primarily for converting numbers into German convention (i.e. decimal comma, point as thousand separator) when there's no client-side code to do so, e.g. when unloading data into text files used for Word imports.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Note: This sample is published as-is - use with care:)&lt;/STRONG&gt;&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;-----------------------------------------------------------------------------
-- Formatting numbers in German convention
--
-- Turns decimal point into decimal comma, sets thousands separator
-- and returns numbers with the according count of decimal places.
--
-- If the input value is not a valid number, NULL is returned.
-----------------------------------------------------------------------------

create function dbo.fnFormatNumber(strValue varchar(100), nCntDecimalPlaces int default 2)
returns varchar(200)
deterministic 
begin 
   declare strReturn varchar(200); 
   declare i integer; 
   declare j integer;

-- Return NULL for non-numeric values
   if isnumeric(strValue) = 0 then
      return null;
   end if;

-- Round/Extend to the given number of decimal places and trim all whitespace
   if nCntDecimalPlaces &amp;lt; 0 then
      set nCntDecimalPlaces = 0;
   end if;

set strValue = str(strValue, 100, nCntDecimalPlaces);
   set strValue = trim(strValue);
   set i = length(strValue); 
   set j = 0;

-- Insert thousand separators
   while i &amp;gt; 0 loop 
      set strReturn = substr(strValue, i, 1) || strReturn; 
      if (locate(strReturn, '.') &amp;gt; 0) or (locate(strValue, '.') = 0) then 
         if substr(strValue, i, 1) &amp;lt;&amp;gt; '.' then 
            set j = j + 1;
         end if; 
      end if; 
      if (j = 3) and (i &amp;gt; 1) then 
         set strReturn = ',' || strReturn; 
         set j = 0;
      end if; 
   set i = i - 1; 
   end loop;

-- Switch point and comma
   set strReturn = replace(strReturn, '.', '|');
   set strReturn = replace(strReturn, ',', '.');
   set strReturn = replace(strReturn, '|', ',');

return strReturn; 
end;

-- Test cases
/*
select
   fnFormatNumber(1234.56),
   fnFormatNumber(1234.00),
   fnFormatNumber(12345678901234567890.1234567890),
   fnFormatNumber(0.00),
   fnFormatNumber(0),
   fnFormatNumber(-.07),
   fnFormatNumber(-1234.5678),
   fnFormatNumber('abcdef'),
   fnFormatNumber(null);

select
   fnFormatNumber(1234.56789, 2),
   fnFormatNumber(1234.56789, 5),
   fnFormatNumber(1234.56789, 0),
   fnFormatNumber(1234.56789, -1),
   fnFormatNumber(1234.56789, 1),
   fnFormatNumber(1234.56789, 3);

select
   fnFormatNumber(1234, 2),
   fnFormatNumber(1234, 5),
   fnFormatNumber(1234, 0),
   fnFormatNumber(1234, -1),
   fnFormatNumber(1234, 1),
   fnFormatNumber(1234, 3);
*/
&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 10 Dec 2010 17:33:10 GMT</pubDate>
    <dc:creator>VolkerBarth</dc:creator>
    <dc:date>2010-12-10T17:33:10Z</dc:date>
    <item>
      <title>How do I format numbers in SQLA</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaq-p/13833385</link>
      <description>&lt;P&gt;I'd like to have something like NUMBERFORMAT analogous to DATEFORMAT, to format a numeric value into a string. Would take a numeric value (integer, double, numeric...) and a format expression (e.g. '#,###.00' as in PowerBuilder).&lt;/P&gt;

&lt;P&gt;Already there? Then tell me where, please ;-).&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2010 12:57:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaq-p/13833385</guid>
      <dc:creator>reimer_pods</dc:creator>
      <dc:date>2010-12-09T12:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I format numbers in SQLA</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833387#M4864230</link>
      <description>&lt;P&gt;Unfortunately not there, AFAIK (but possibly planned for future versions)... May the questions &lt;A href="http://sqlanywhere-forum.sap.com/questions/335"&gt;http://sqlanywhere-forum.sap.com/questions/335&lt;/A&gt; and &lt;A href="http://sqlanywhere-forum.sap.com/questions/394"&gt;http://sqlanywhere-forum.sap.com/questions/394&lt;/A&gt; help?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2010 13:50:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833387#M4864230</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-12-09T13:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I format numbers in SQLA</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833386#M4864229</link>
      <description>&lt;P&gt;Just a function you might like to use. It's not exactly what you are looking for (and doesn't add leading zeroes) but could help as a starting point.&lt;/P&gt;

&lt;P&gt;I use this primarily for converting numbers into German convention (i.e. decimal comma, point as thousand separator) when there's no client-side code to do so, e.g. when unloading data into text files used for Word imports.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Note: This sample is published as-is - use with care:)&lt;/STRONG&gt;&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;-----------------------------------------------------------------------------
-- Formatting numbers in German convention
--
-- Turns decimal point into decimal comma, sets thousands separator
-- and returns numbers with the according count of decimal places.
--
-- If the input value is not a valid number, NULL is returned.
-----------------------------------------------------------------------------

create function dbo.fnFormatNumber(strValue varchar(100), nCntDecimalPlaces int default 2)
returns varchar(200)
deterministic 
begin 
   declare strReturn varchar(200); 
   declare i integer; 
   declare j integer;

-- Return NULL for non-numeric values
   if isnumeric(strValue) = 0 then
      return null;
   end if;

-- Round/Extend to the given number of decimal places and trim all whitespace
   if nCntDecimalPlaces &amp;lt; 0 then
      set nCntDecimalPlaces = 0;
   end if;

set strValue = str(strValue, 100, nCntDecimalPlaces);
   set strValue = trim(strValue);
   set i = length(strValue); 
   set j = 0;

-- Insert thousand separators
   while i &amp;gt; 0 loop 
      set strReturn = substr(strValue, i, 1) || strReturn; 
      if (locate(strReturn, '.') &amp;gt; 0) or (locate(strValue, '.') = 0) then 
         if substr(strValue, i, 1) &amp;lt;&amp;gt; '.' then 
            set j = j + 1;
         end if; 
      end if; 
      if (j = 3) and (i &amp;gt; 1) then 
         set strReturn = ',' || strReturn; 
         set j = 0;
      end if; 
   set i = i - 1; 
   end loop;

-- Switch point and comma
   set strReturn = replace(strReturn, '.', '|');
   set strReturn = replace(strReturn, ',', '.');
   set strReturn = replace(strReturn, '|', ',');

return strReturn; 
end;

-- Test cases
/*
select
   fnFormatNumber(1234.56),
   fnFormatNumber(1234.00),
   fnFormatNumber(12345678901234567890.1234567890),
   fnFormatNumber(0.00),
   fnFormatNumber(0),
   fnFormatNumber(-.07),
   fnFormatNumber(-1234.5678),
   fnFormatNumber('abcdef'),
   fnFormatNumber(null);

select
   fnFormatNumber(1234.56789, 2),
   fnFormatNumber(1234.56789, 5),
   fnFormatNumber(1234.56789, 0),
   fnFormatNumber(1234.56789, -1),
   fnFormatNumber(1234.56789, 1),
   fnFormatNumber(1234.56789, 3);

select
   fnFormatNumber(1234, 2),
   fnFormatNumber(1234, 5),
   fnFormatNumber(1234, 0),
   fnFormatNumber(1234, -1),
   fnFormatNumber(1234, 1),
   fnFormatNumber(1234, 3);
*/
&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 10 Dec 2010 17:33:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833386#M4864229</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-12-10T17:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do I format numbers in SQLA</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833388#M4864231</link>
      <description>&lt;P&gt;A further note: This code may be based on contributions from the &lt;EM&gt;SQL Anywhere NNTP groups&lt;/EM&gt; years ago. I'm not sure as I would usually have written down an according note, and here I haven't...&lt;/P&gt;</description>
      <pubDate>Fri, 10 Dec 2010 17:49:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833388#M4864231</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2010-12-10T17:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I format numbers in SQLA</title>
      <link>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833389#M4864232</link>
      <description>&lt;P&gt;Thanx, Volker! I'd still like to be able to specify a format string. But I could use this function in some cases to replace a similar function we've written that doesn't insert thousand separators.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Dec 2010 11:51:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-do-i-format-numbers-in-sqla/qaa-p/13833389#M4864232</guid>
      <dc:creator>reimer_pods</dc:creator>
      <dc:date>2010-12-13T11:51:20Z</dc:date>
    </item>
  </channel>
</rss>

