【发布时间】:2020-01-14 06:04:57
【问题描述】:
有没有一种安全快速的方法将数字转换为 varchar2 而无需千位分隔符(无论排序规则或其他设置如何)。
例子:
/* Normal Collation */
1234.5 => '1234.5' (and not '1,234.5')
1.5 => '1.5'
/* Reverse Collation */
1234.5 => '1234,5' (and not '1.234,5')
1.5 => '1,5'
到目前为止我所拥有的是:
declare
thousand_separator# varchar2(1);
begin
thousand_separator# := nullif(substr(to_char(1000), 2, 1), 0);
dbms_output.put_line(case when thousand_separator# is null then to_char(123456.123) else replace(to_char(123456.123), thousand_separator#) end);
end;
我正在寻找 to_char(<number>, 'CORRECT_FORMAT') 或类似类型的东西(不会添加任何前导零或尾随零)
【问题讨论】:
-
在您的情况下,表达式
thousand_separator#永远不能为空,因为您通过nvl()分配它。为什么你不能只使用不包含组分隔符的格式掩码(例如,'99999D99999' 在小数点分隔符的两边都有足够的 9,用于您希望看到的可能值范围 - as here)?跨度> -
是否存在默认无法正确显示的值?我唯一能立即想到的是 - 低于零(例如 0.5)的数字应该如何显示?
-
@AlexPoole 我的意思是写
nullif而不是nvl。
标签: sql oracle plsql type-conversion