to_char(number) function in postgres

只谈情不闲聊 提交于 2019-11-29 02:02:30

问题


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

In oracle i can write like

SELECT to_char(1234) FROM DUAL

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


回答1:


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

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:


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




回答3:


CAST function worked for me.

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



回答4:


You can use:

1234||''

It works on most databases.



来源:https://stackoverflow.com/questions/14155656/to-charnumber-function-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!