Re: Extra space when converting number with to_char

From: David Johnston <polobo(at)yahoo(dot)com>
To: Samuel Gilbert <samuel(dot)gilbert(at)ec(dot)gc(dot)ca>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Extra space when converting number with to_char
Date: 2012-10-26 19:23:50
Message-ID: 4E221562-C15B-4F34-BE81-7C987907FF43@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Placeholder for a +/- sign so that both positive and negative values have equal lengths.

David J.

On Oct 26, 2012, at 14:50, Samuel Gilbert <samuel(dot)gilbert(at)ec(dot)gc(dot)ca> wrote:

> Hello,
>
> I've encountered an unexpected behavior when converting a number to char. I
> need to match an INTEGER with a VARCHAR(9) that has leading zeros. When I
> tried to match them using to_char(num, '000000000') I didn't get anything. I
> later found out that to_char adds a leading space. This is demonstrated with
> the query below :
>
> SELECT
> string,
> length(string),
> '"' || string || '"' AS showThemToMe
> FROM
> (SELECT to_char(42, '000000000') AS string) AS example;
>
> string | length | showthemtome
> ------------+--------+--------------
> 000000042 | 10 | " 000000042"
>
> Is there a reason why to_char adds a leading space? Is this a bug? I can
> easily fix this with trim(leading ' ' from to_char(num, '000000000')), but,
> being of a curious nature, I'd like ton know why I need to do that.
>
> --
> Samuel Gilbert
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bosco Rama 2012-10-26 19:58:36 Re: Extra space when converting number with to_char
Previous Message Samuel Gilbert 2012-10-26 18:50:15 Extra space when converting number with to_char