to_char(number) function in postgres

2019-02-03 02:39发布

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.

4条回答
太酷不给撩
2楼-- · 2019-02-03 03:17

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
查看更多
Summer. ? 凉城
3楼-- · 2019-02-03 03:25

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

查看更多
Deceive 欺骗
4楼-- · 2019-02-03 03:33

You can use:

1234||''

It works on most databases.

查看更多
爷、活的狠高调
5楼-- · 2019-02-03 03:34

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
查看更多
登录 后发表回答