char(8) vs char8

Started by Nonameover 27 years ago6 messages
#1Noname
darcy@druid.net

This was an unexpected difference between these two types and I wonder
if it was meant to be this way. Previously, a char8 field with the
string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module
or the C interface (which my module uses, of course.) This causes a
lot of my programs to break.

I have made a quick change to my Python module to handle this. Should
I clean it up or can I expect the behaviour to go back the way it was?

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#2Byron Nikolaidis
byronn@insightdist.com
In reply to: Noname (#1)
Re: [HACKERS] char(8) vs char8

D'Arcy J.M. Cain wrote:

This was an unexpected difference between these two types and I wonder
if it was meant to be this way. Previously, a char8 field with the
string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module
or the C interface (which my module uses, of course.) This causes a
lot of my programs to break.

char(x) is the datatype 'bpchar' (blank padded char). Thus it is padded
with spaces to the field width.

Couldn't you use something like "select rtrim(column) from table". This
will trim the spaces off.

Byron

#3Andreas Zeugswetter
andreas.zeugswetter@telecom.at
In reply to: Byron Nikolaidis (#2)
AW: [HACKERS] char(8) vs char8

This was an unexpected difference between these two types and I wonder
if it was meant to be this way. Previously, a char8 field with the
string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module
or the C interface (which my module uses, of course.) This causes a
lot of my programs to break.

This is the expected behavior for the char() datatype. If you have other variable
length fields in the table, you can simply use varchar() which does not blank pad
to the specified length. If you don't have other variable length fields then you
loose some performance if you switch to varchar().

I have made a quick change to my Python module to handle this. Should
I clean it up or can I expect the behaviour to go back the way it was?

No, it will stay.

Andreas

#4Thomas G. Lockhart
lockhart@alumni.caltech.edu
In reply to: Noname (#1)
Re: [HACKERS] char(8) vs char8

This was an unexpected difference between these two types and I wonder
if it was meant to be this way. Previously, a char8 field with the
string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module
or the C interface (which my module uses, of course.) This causes a
lot of my programs to break.

I have made a quick change to my Python module to handle this. Should
I clean it up or can I expect the behaviour to go back the way it was?

The behavior you want is varchar() rather than char().

- Tom

#5Noname
darcy@druid.net
In reply to: Byron Nikolaidis (#2)
Re: [HACKERS] char(8) vs char8

Thus spake Byron Nikolaidis

string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module

Couldn't you use something like "select rtrim(column) from table". This
will trim the spaces off.

It wouldn't be as convenient as this example.

import pg
for d in pg.connect('database').query('select * from table').dictresult():
print "Num: %(field1)3d, '%(field2)s'" % d

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#6Noname
darcy@druid.net
In reply to: Thomas G. Lockhart (#4)
Re: [HACKERS] char(8) vs char8

Thus spake Thomas G. Lockhart

string 'abc' would return 'abc' as expected. Now, with char(8), I get
back 'abc ' instead. You can see this with my PygreSQL module

The behavior you want is varchar() rather than char().

Right. Dump and reload time I guess. >:-/

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.