Re: CAST(integer_field AS character) truncates trailing zeros

From: "Douglas McNaught" <doug(at)mcnaught(dot)org>
To: "Richard Broersma" <richard(dot)broersma(at)gmail(dot)com>
Cc: "Warren Bell" <warren(at)clarksnutrition(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: CAST(integer_field AS character) truncates trailing zeros
Date: 2008-07-31 20:44:04
Message-ID: 5ded07e00807311344l2bbdcfa5x876a57cc62c823e0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jul 31, 2008 at 4:17 PM, Richard Broersma
<richard(dot)broersma(at)gmail(dot)com> wrote:
> On Thu, Jul 31, 2008 at 1:03 PM, Warren Bell <warren(at)clarksnutrition(dot)com> wrote:
>> I am trying to cast an int to a character. The int is the number 1000 it
>> gets cast down to "1" and not "1000". How do I cast from int to character
>> without loosing the trailing zeros?
>
> Here is what I get when I try:
>
> postgres=# select cast( cast( 1000 as integer ) as char );
> bpchar
> --------
> 1
> (1 row)

AFAIK, CHAR means CHAR(1), so there are not enough characters to
contain the result and it gets truncated. Maybe TEXT should be used
instead, or VARCHAR?

-Doug

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Warren Bell 2008-07-31 21:05:58 Re: CAST(integer_field AS character) truncates trailing zeros
Previous Message Lennin Caro 2008-07-31 20:42:00 Re: CAST(integer_field AS character) truncates trailing zeros