You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PL/SQL中按英式/英联邦规范将金额转为英文单词

Converting Numeric Amounts to British English Word Format in Oracle

Great question! Oracle's Julian date trick (TO_DATE(Amt,'J') paired with TO_CHAR(..., 'JSP')) is a handy way to turn numbers into words, but it defaults to American English—missing that critical "and" that British English uses between hundreds and tens/units, or after thousands when there's a remaining lower amount.

Your Initial Case-Based Implementation

You first tackled this with a CASE statement to handle specific scenarios like pure hundreds, thousands without hundreds, and thousands with hundreds:

SELECT 
    TO_CHAR(TO_DATE(CEIL(MAX(A.Amt)),'J' ),'Jsp') AS AMERICAN_ENGLISH, 
    CASE 
        WHEN TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') LIKE '%Hundred%' 
             AND TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') NOT LIKE '%Thousand%' 
        THEN REGEXP_replace(TO_CHAR(TO_DATE(CEIL(MAX(A.Amt)),'J' ),'Jsp'),'\s',' and ',1,2)
        WHEN TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') LIKE '%Thousand%' 
             AND TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') NOT LIKE '%Hundred%' 
             AND CEIL(MAX(A.Amt)) > 1000 
        THEN REGEXP_replace(TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') ,'\s',' and ',1,2)
        WHEN TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') LIKE '%Hundred%' 
             AND TO_CHAR(TO_DATE(CEIL(mAX(A.Amt)),'J' ),'Jsp') LIKE '%Thousand%' 
        THEN REGEXP_replace(TO_CHAR(TO_DATE(CEIL(MAX(A.Amt)),'J' ),'Jsp'),'\s',' and ',1,4)
        ELSE TO_CHAR(TO_DATE(CEIL(MAX(A.Amt)),'J' ),'Jsp') 
    END AS BRITISH_ENGLISH 
FROM TABLE1 A

This works for targeted cases, but you rightly recognized it could be simplified to cover all scenarios.

Streamlined, Universal Solution

Your optimized version is brilliant because it adapts to all amount sizes without hardcoding case logic. By using INSTR to find the last space in the American English string and replacing that single space with " and ", you perfectly align with British English rules:

SELECT 
    REGEXP_replace(
        TO_CHAR(TO_DATE(CEIL(A.Amt), 'J'), 'Jsp'), 
        ' ', 
        ' and ',
        instr(TO_CHAR(TO_DATE(CEIL(A.Amt), 'J'), 'Jsp'),' ',-1)
    ) AS BRITISH_ENGLISH
FROM TABLE A

Here's why this works so well:

  • instr(..., ' ', -1) locates the position of the final space in the worded number (e.g., in "five hundred twelve", it finds the space between "hundred" and "twelve"; in "one thousand five hundred twelve", it finds the space between "hundred" and "twelve").
  • We only replace that one space with " and ", which adds the required conjunction exactly where British English needs it—no matter how large the number is.

内容的提问来源于stack exchange,提问作者WhiteRose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:47:31