我想使用千位分隔符格式化长数字。可以使用 to_char
函数来完成,就像:
SELECT TO_CHAR(76543210.98, '999G999G990D00')
但是当我使用 UTF-8 编码的 PostgreSQL 服务器在波兰语版本的 Windows 上时,这样的 SELECT 以:
ERROR: invalid byte sequence for encoding "UTF8": 0xa0
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
在 to_char
模式中,G
被描述为:组分隔符(使用区域设置)。
当服务器在具有波兰语语言环境的 Linux 上运行时,此 SELECT 可以正常工作。
作为一种解决方法,我在格式字符串中使用空格而不是 G
,但我认为应该有办法像在 Oracle 中一样设置千位分隔符:
ALTER SESSION SET NLS_NUMERIC_CHARACTERS=', ';
PostgreSQL 可以使用这样的设置吗?
最佳答案
如果你使用psql
,你可以这样执行:
\pset numericlocale
例子:
test=# create temporary table a (a numeric(20,10));
CREATE TABLE
test=# insert into a select random() * 1000000 from generate_series(1,3);
INSERT 0 3
test=# select * from a;
a
-------------------
287421.6944910590
140297.9311533270
887215.3805568810
(3 rows)
test=# \pset numericlocale
Showing locale-adjusted numeric output.
test=# select * from a;
a
--------------------
287.421,6944910590
140.297,9311533270
887.215,3805568810
(3 rows)
https://stackoverflow.com/questions/3360752/