Wednesday 26 November 2014

Convert Amount into words in SQL

function convert_number (in_numeral integer := 0) return varchar2  is
  number_too_large    exception;
  numeral             integer := abs(trunc(in_numeral));
  max_digit           integer := 12;  -- for numbers less than a trillion
  number_text         varchar2(580) := ''; -- Lenght increased from 240 to 480 for UTF8
  current_segment     varchar2(80);
  b_zero              varchar2(80);        -- Lenght increased to 80 for UTF8
  b_thousand          varchar2(80);        -- Lenght increased to 80 for UTF8
  thousand            number      := power(10,3);
  b_million           varchar2(80) ;        -- Lenght increased to 80 for UTF8
  million             number      := power(10,6);
  b_billion           varchar2(80);        -- Lenght increased to 80 for UTF8
  billion             number      := power(10,9);
  v_paisa_amount            number      ;
  v_paisa_char                varchar2(100);
   

  function convert_number (segment number) return varchar2 is
    value_text  varchar2(80);
  begin
    value_text := to_char( to_date(segment,'YYYY'),'Yyyysp');
    return(value_text);
  end;

begin

  select abs(round(mod(in_numeral,trunc(in_numeral)) * 100)) into v_paisa_amount from dual;


  if numeral >= power(10,max_digit) then
     raise number_too_large;
  end if;




--    Here u can use Billion, Million, Thousand in place of query

  select   ' '||lc1.displayed_field||' ',
           ' '||lc2.displayed_field||' ',
           ' '||lc3.displayed_field||' ',
           ' '||lc4.displayed_field
  into     b_billion,
           b_million,
           b_thousand,
           b_zero
  from     ap_lookup_codes lc1,
           ap_lookup_codes lc2,
           ap_lookup_codes lc3,
           ap_lookup_codes lc4
  where    lc1.lookup_code = 'BILLION'
  and      lc1.lookup_type = 'NLS TRANSLATION'
  and      lc2.lookup_code = 'MILLION'
  and      lc2.lookup_type = 'NLS TRANSLATION'
  and      lc3.lookup_code = 'THOUSAND'
  and      lc3.lookup_type = 'NLS TRANSLATION'
  and      lc4.lookup_code = 'ZERO'
  and      lc4.lookup_type = 'NLS TRANSLATION';


  if numeral = 0 then
     return(b_zero);
  end if;

  current_segment := trunc(numeral/billion);
  numeral := numeral - (current_segment * billion);
    if current_segment != 0 then
     number_text := number_text||convert_number(current_segment)||b_billion;
  end if;

  current_segment := trunc(numeral/million);
  numeral := numeral - (current_segment * million);
  if current_segment != 0 then
     number_text := number_text||convert_number(current_segment)||b_million;
  end if;

  current_segment := trunc(numeral/thousand);
  numeral := numeral - (current_segment * thousand);
  if current_segment != 0 then
     number_text := number_text||convert_number(current_segment)||b_thousand;
  end if;

  if numeral != 0 then
     number_text := number_text||convert_number(numeral);
  end if;

  number_text := substr(number_text,1,1) ||
                 rtrim(lower(substr(number_text,2,NVL(length(number_text), 0))));                
                
              
  if v_paisa_amount != 0 then
      v_paisa_char := to_char( to_date(v_paisa_amount,'YYYY'),'Yyyysp');
      number_text := number_text||' And '||v_paisa_char||' Paisas';
  end if;

   number_text := number_text || ' Only ';

  return(number_text);

RETURN NULL; exception
  when number_too_large then
        return(null);
  when others then
        return(null);
end;

No comments:

Post a Comment