使用 PL SQL,我需要將數字格式化為 VARCHAR2。如果它們不為零,我需要顯示小數位。小數點分隔符應該是逗號。
| 輸入 | 輸出 |
|---|---|
| 0.2 | '0,2' |
| 100.4 | '100,4' |
| 22 | '22' |
uj5u.com熱心網友回復:
這樣的事情會做嗎?
SQL> with test (input) as
2 (select 0.2 from dual union all
3 select 100.4 from dual union all
4 select 22 from dual
5 )
6 select input,
7 case when input = trunc(input) then to_char(input, '999G990')
8 else to_char(input, '999G990D0')
9 end output
10 from test;
INPUT OUTPUT
---------- ----------
,2 0,2
100,4 100,4
22 22
SQL>
uj5u.com熱心網友回復:
這是獨立于語言環境的,不限制或舍入數字
with
test(input) as (
select 0.2 from dual union all
select 100.4 from dual union all
select -12.34567 from dual union all
select 1.23e34 from dual union all
select -1.23e-34 from dual union all
select 22 from dual
)
select input
,case when input < 0 then '-' end -- sign
||case when abs(input) < 1 then '0' end -- zero
||replace(to_char(abs(input)),to_char(0,'fmd'),',') -- comma
output
from test
/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414332.html
標籤:
