Extra space when converting number with to_char - Mailing list pgsql-general

From Samuel Gilbert
Subject Extra space when converting number with to_char
Date
Msg-id 201210261450.15855.samuel.gilbert@ec.gc.ca
Whole thread Raw
Responses Re: Extra space when converting number with to_char  (David Johnston <polobo@yahoo.com>)
Re: Extra space when converting number with to_char  (Bosco Rama <postgres@boscorama.com>)
Re: Extra space when converting number with to_char  (Moshe Jacobson <moshe@neadwerx.com>)
List pgsql-general
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


pgsql-general by date:

Previous
From: D T
Date:
Subject: Function Profiler in Postgre
Next
From: David Johnston
Date:
Subject: Re: Extra space when converting number with to_char