如何在PL/SQL中按英式/英联邦规范将金额转为英文单词
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

