不确定它是否更优雅,但假设您的替换是处理不同的语言环境,这可能对您有用:
with t as (
select 1.00 as n from dual
union all select 1.001 from dual
union all select 0.203 from dual
union all select 0.2345 from dual
union all select 112.999 from dual
)
select n, regexp_replace(to_char(trunc(n, 2), '9999999990D00',
'NLS_NUMERIC_CHARACTERS='',.'''), '[,.]00$', null) as new_n
from t;
N NEW_N
---------- --------------
1 1
1.001 1
0.203 0,20
0.2345 0,23
112.999 112,99
让您指定它是使用逗号还是句点作为小数分隔符的参数nls_param
。to_char
如果您可以在会话级别设置它,那么查询看起来会更简单一些。字符串末尾的regexp_replace
条带,00
(或.00
,认为它是矫枉过正的)。
正如 ThinkJet 所指出的regexp_replace
,这有点过分,并且由于小数分隔符是在列中定义的(并且格式无论如何都没有组分隔符),因此可以通过计划来完成replace
:
with t as (
select 1.00 as n from dual
union all select 1.001 from dual
union all select 0.203 from dual
union all select 0.2345 from dual
union all select 112.999 from dual
union all select 13.08 from dual
)
select n, replace(trim(
to_char(trunc(n, 2), '9999999990D00', 'NLS_NUMERIC_CHARACTERS='',.''')),
',00', null) as new_n
from t;
N NEW_N
---------- --------------
1 1
1.001 1
0.203 0,20
0.2345 0,23
112.999 112,99
13.08 13,08
不过,仍然不确定这是否可以被描述为“优雅”。