postgres中的to_char(数字)函数

job*_*i88 22 postgresql postgresql-9.1

我想使用to_char()函数显示/转换一个数字(它的长度相同).

在oracle我可以写得像

SELECT to_char(1234) FROM DUAL
Run Code Online (Sandbox Code Playgroud)

但是在postgres SELECT to_char(1234) 中没有用.

a_h*_*ame 41

您需要提供格式掩码.在PostgreSQL中没有默认值:

select to_char(1234, 'FM9999');
Run Code Online (Sandbox Code Playgroud)

如果您不知道有多少位数,只需估算最大值:

select to_char(1234, 'FM999999999999999999');
Run Code Online (Sandbox Code Playgroud)

如果数字的位数较少,则不会产生任何副作用.

如果您不需要任何格式(如小数点,千位分隔符),您还可以简单地将值转换为文本:

select 1234::text
Run Code Online (Sandbox Code Playgroud)


小智 6

你必须指定一种数字格式,即:

to_char(1234, '9999')
Run Code Online (Sandbox Code Playgroud)

在这里查看更多信息:http://www.postgresql.org/docs/current/static/functions-formatting.html