在postgres to_char()函数

时间:2022-02-09 15:48:19

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中,选择to_char(1234)是无效的。

3 个解决方案

#1


34  

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

#2


4  

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。

#3


0  

You can use:

您可以使用:

1234||''

It works on most databases.

它适用于大多数数据库。

#1


34  

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

#2


4  

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。

#3


0  

You can use:

您可以使用:

1234||''

It works on most databases.

它适用于大多数数据库。