dbTalk Databases Forums  

to_char issue

comp.databases.postgresql comp.databases.postgresql


Discuss to_char issue in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Anselmo Canfora
 
Posts: n/a

Default to_char issue - 02-03-2010 , 10:25 AM






Hi, is this a bug or am I misunderstanding something?

db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...

Reply With Quote
  #2  
Old   
Anselmo Canfora
 
Posts: n/a

Default Re: to_char issue - 02-03-2010 , 10:26 AM






Il 03/02/2010 17.25, Anselmo Canfora ha scritto:
Quote:
Hi, is this a bug or am I misunderstanding something?

db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...
mmm... check this one:

db_atm=# select '|' || to_char(552020, '000000000') || '|' as culptrit;
culptrit
--------------
Quote:
000552020|
(1 row)

Reply With Quote
  #3  
Old   
Marco Mariani
 
Posts: n/a

Default Re: to_char issue - 02-03-2010 , 10:30 AM



On 02/03/2010 05:26 PM, Anselmo Canfora wrote:

Quote:
db_atm=# select '|' || to_char(552020, '000000000') || '|' as culptrit;
culptrit
--------------
| 000552020|
(1 row)
I suppose it's for the '-' sign on negative numbers.

Reply With Quote
  #4  
Old   
Marco Mariani
 
Posts: n/a

Default Re: to_char issue - 02-03-2010 , 10:35 AM



On 02/03/2010 05:25 PM, Anselmo Canfora wrote:
Quote:
Hi, is this a bug or am I misunderstanding something?

db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...
Yep, it's documented:

http://www.postgresql.org/docs/8.1/s...ormatting.html

FM suppresses leading zeroes and trailing blanks that would otherwise be
added to make the output of a pattern be fixed-width.

So you should use 'FM000000000'

Reply With Quote
  #5  
Old   
Anselmo Canfora
 
Posts: n/a

Default Re: to_char issue - 02-03-2010 , 03:35 PM



Il 03/02/2010 17.35, Marco Mariani ha scritto:
Quote:
On 02/03/2010 05:25 PM, Anselmo Canfora wrote:
Hi, is this a bug or am I misunderstanding something?

db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...

Yep, it's documented:

http://www.postgresql.org/docs/8.1/s...ormatting.html

FM suppresses leading zeroes and trailing blanks that would otherwise be
added to make the output of a pattern be fixed-width.

So you should use 'FM000000000'
rather strange, this behavior it is not really clear from documentation,
"that would otherwise be added to make the output of a pattern be
fixed-width" it is a part not really clear for me, in my example the
pattern is one of 9 chars, to_char is adding a blank, giving a 10 char
wide string, I don't get the reason of such implementation.

Reply With Quote
  #6  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: to_char issue - 02-04-2010 , 01:59 AM



Anselmo Canfora wrote:
Quote:
db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...

Yep, it's documented:

http://www.postgresql.org/docs/8.1/s...ormatting.html

FM suppresses leading zeroes and trailing blanks that would otherwise be
added to make the output of a pattern be fixed-width.

So you should use 'FM000000000'

rather strange, this behavior it is not really clear from documentation, "that would otherwise be added to make the output of a
pattern be fixed-width" it is a part not really clear for me, in my example the pattern is one of 9 chars, to_char is adding a
blank, giving a 10 char wide string, I don't get the reason of such implementation.
Distinguish between "the pattern" and "the output of a pattern".

For "to_char(-1, '0')" you get "-1", so to make the output of the
pattern fixed-with no matter what the input is, "to_char(1, '0')"
will produce " 1".

The reason for the implementation is easily given:
This is an Oracle compatibility function, so it will behave like
Oracle as much as it can.

If you think that the behaviour is silly, file an enhancement request
with Oracle and come back when they have changed it :^)

Yours,
Laurenz Albe

Reply With Quote
  #7  
Old   
Anselmo Canfora
 
Posts: n/a

Default Re: to_char issue - 02-04-2010 , 02:49 AM



Il 04/02/2010 8.59, Laurenz Albe ha scritto:
Quote:
Anselmo Canfora wrote:
db_atm=# select length(to_char(552020, '000000000'));
length
--------
10
(1 row)

in my opinion it should give 9... I am getting ugly errors for this...

Yep, it's documented:

http://www.postgresql.org/docs/8.1/s...ormatting.html

FM suppresses leading zeroes and trailing blanks that would otherwise be
added to make the output of a pattern be fixed-width.

So you should use 'FM000000000'

rather strange, this behavior it is not really clear from documentation, "that would otherwise be added to make the output of a
pattern be fixed-width" it is a part not really clear for me, in my example the pattern is one of 9 chars, to_char is adding a
blank, giving a 10 char wide string, I don't get the reason of such implementation.

Distinguish between "the pattern" and "the output of a pattern".

For "to_char(-1, '0')" you get "-1", so to make the output of the
pattern fixed-with no matter what the input is, "to_char(1, '0')"
will produce " 1".
now it is clear

Quote:
The reason for the implementation is easily given:
This is an Oracle compatibility function, so it will behave like
Oracle as much as it can.

If you think that the behaviour is silly, file an enhancement request
with Oracle and come back when they have changed it :^)
the next time I'll see Ellison's yacht in my city harbour will jump
aboard and yell about that

Quote:
Yours,
Laurenz Albe

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.