Oracle取的表中小寫金額的大寫形式

主要思路

  • 創建一個進行數字轉換的function
  • 再原有表的基礎上創建一個包含利用此Function生成大寫字段

1.

Create Or Replace Function Money2Chinese(Money In Number) Return Varchar2 Is

  strYuan Varchar2(150);

  strYuanFen Varchar2(152);

  numLenYuan Number;

  numLenYuanFen Number;

  strRstYuan Varchar2(600);

  strRstFen Varchar2(200);

  strRst Varchar2(800);

  Type typeTabMapping Is Table Of Varchar2(2) Index By Binary_Integer;

  tabNumMapping typeTabMapping;

  tabUnitMapping typeTabMapping;

  numUnitIndex Number;

  i Number;

  j Number;

  charCurrentNum Char(1);
Begin

  If Money Is Null Then
  
    Return Null;
  
  End If;

  strYuan := TO_CHAR(FLOOR(Money));

  If strYuan = '0' Then
  
    numLenYuan := 0;
  
    strYuanFen := lpad(TO_CHAR(FLOOR(Money * 100)), 2, '0');
  
  Else
  
    numLenYuan := length(strYuan);
  
    strYuanFen := TO_CHAR(FLOOR(Money * 100));
  
  End If;

  If strYuanFen = '0' Then
  
    numLenYuanFen := 0;
  
  Else
  
    numLenYuanFen := length(strYuanFen);
  
  End If;

  If numLenYuan = 0 Or numLenYuanFen = 0 Then
  
    strRst := '零圓整';
  
    Return strRst;
  
  End If;

  tabNumMapping(0) := '零';

  tabNumMapping(1) := '壹';

  tabNumMapping(2) := '貳';

  tabNumMapping(3) := '叁';

  tabNumMapping(4) := '肆';

  tabNumMapping(5) := '伍';

  tabNumMapping(6) := '陸';

  tabNumMapping(7) := '柒';

  tabNumMapping(8) := '捌';

  tabNumMapping(9) := '玖';

  tabUnitMapping(-2) := '分';

  tabUnitMapping(-1) := '角';

  tabUnitMapping(1) := '';

  tabUnitMapping(2) := '拾';

  tabUnitMapping(3) := '佰';

  tabUnitMapping(4) := '仟';

  tabUnitMapping(5) := '萬';

  tabUnitMapping(6) := '拾';

  tabUnitMapping(7) := '佰';

  tabUnitMapping(8) := '仟';

  tabUnitMapping(9) := '億';

  For i In 1 .. numLenYuan Loop
  
    j := numLenYuan - i + 1;
  
    numUnitIndex := Mod(i, 8);
  
    If numUnitIndex = 0 Then
    
      numUnitIndex := 8;
    
    End If;
  
    If numUnitIndex = 1 And i > 1 Then
    
      strRstYuan := tabUnitMapping(9) || strRstYuan;
    
    End If;
  
    charCurrentNum := substr(strYuan, j, 1);
  
    If charCurrentNum <> 0 Then
    
      strRstYuan := tabNumMapping(charCurrentNum) ||
                   
                    tabUnitMapping(numUnitIndex) || strRstYuan;
    
    Else
    
      If (i = 1 Or i = 5) Then
      
        If substr(strYuan, j - 3, 4) <> '0000' Then
        
          strRstYuan := tabUnitMapping(numUnitIndex) || strRstYuan;
        
        End If;
      
      Else
      
        If substr(strYuan, j + 1, 1) <> '0' Then
        
          strRstYuan := tabNumMapping(charCurrentNum) || strRstYuan;
        
        End If;
      
      End If;
    
    End If;
  
  End Loop;

  For i In -2 .. -1 Loop
  
    j := numLenYuan - i;
  
    charCurrentNum := substr(strYuanFen, j, 1);
  
    If charCurrentNum <> '0' Then
    
      strRstFen := tabNumMapping(charCurrentNum) || tabUnitMapping(i) ||
                  
                   strRstFen;
    
    End If;
  
  End Loop;

  If strRstYuan Is Not Null Then
  
    strRstYuan := strRstYuan || '圓';
  
  End If;

  If strRstFen Is Null Then
  
    strRstYuan := strRstYuan || '整';
  
  Elsif length(strRstFen) = 2 And substr(strRstFen, 2) = '角' Then
  
    strRstFen := strRstFen || '整';
  
  End If;

  strRst := strRstYuan || strRstFen;

  --strRst := Replace(strRst, '億零', '億');

  --strRst := Replace(strRst, '萬零', '萬');

  Return strRst;

End Money2Chinese;

2.

create or replace view vw_money as select t.*,Money2Chinese(t.recmoney) bigmoney from feedetail t

其中t.recmoney即為表中需要轉換為大寫的數據

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,967評論 19 139
  • 1、引言 數據庫設計過程中表、字段等的命名規范也算是設計規范的一部分,不過設計規范更多的是為了確保數據庫設計的合理...
    SnowflakeCloud閱讀 41,108評論 0 48
  • 本以為,那些塵封的記憶 連同歲月和你 都一起留在荒蕪的時光里 埋在層層的黃沙中 從此,日子一片風輕云淡 各自為安 ...
    又見依依閱讀 396評論 13 22
  • ——你想他嗎? ——想,我好想他! 今天是星期六,一個電話把我吵醒了,起來看到窗外才發現還是大雨,不知道是不是因為...
    YU元氣少女閱讀 183評論 0 0