jobi88
jobi88

Reputation: 4225

to_char(number) function in postgres

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.

Upvotes: 34

Views: 116433

Answers (4)

changed
changed

Reputation: 2143

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

Upvotes: 2

Eduardo
Eduardo

Reputation: 2341

You can use:

1234||''

It works on most databases.

Upvotes: -1

user330315
user330315

Reputation:

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

Upvotes: 61

Lorenzo L
Lorenzo L

Reputation: 201

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

Upvotes: 8

Related Questions