postgresql - 如何为 PostgreSQL 设置千位分隔符?

我想使用千位分隔符格式化长数字。可以使用 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/

相关文章:

php - 如何创建友好的日期格式(例如 "submitted 2 days ago")

formatting - Fortran 中带有前导零的格式化输出

c# - 在 Visual Studio 201x 中关闭 #region 的自动格式化

c# - 用逗号代替小数将数字格式化为字符串

formatting - printf, sprintf 至少打印两位小数

java - 删除 Eclipse 中的尾随空格——来自评论

ide - 保存文件时禁用重新格式化代码

javascript - 如何更改 winston 日志格式?

javascript - 在格式化数字输入时验证数字输入

c# - 如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?