postgresql postgres 中的 to_char(number) 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14155656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:56:53  来源:igfitidea点击:

to_char(number) function in postgres

postgresqlpostgresql-9.1

提问by jobi88

i want to display/convert a number to character (of it's same length) using to_char() function .

我想使用 to_char() 函数将数字显示/转换为字符(长度相同)。

In oracle i can write like

在oracle中我可以这样写

SELECT to_char(1234) FROM DUAL

But in postgres SELECT to_char(1234)is not working.

但是在 postgres SELECT to_char(1234)中不起作用。

回答by a_horse_with_no_name

You need to supply a format mask. In PostgreSQL there is no default:

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

select to_char(1234, 'FM9999');

If you don't know how many digits there are, just estimate the maximum:

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

select to_char(1234, 'FM999999999999999999');

If the number has less digits, this won't have any side effects.

如果数字的位数较少,则不会有任何副作用。

If you don't need any formatting (like decimal point, thousands separator) you can also simply cast the value to text:

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

select 1234::text

回答by Lorenzo L

you have to specify a numeric format, ie:

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

to_char(1234, '9999')

Take a look here for more info: http://www.postgresql.org/docs/current/static/functions-formatting.html

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

回答by changed

CAST function worked for me.

CAST 功能对我有用。

SELECT CAST(integerv AS text) AS textv
FROM (
       SELECT 1234 AS integerv
     ) x

OR

或者

SELECT integerv::text AS textv
FROM (
       SELECT 1234 AS integerv
     ) x

回答by Eduardo

You can use:

您可以使用:

1234||''

It works on most databases.

它适用于大多数数据库。