Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / August 2003

Tip: Looking for answers? Try searching our database.

Handling of trailing spaces when reading CHAR fields

Thread view: 
Benjamin Lorenz - 25 Aug 2003 21:33 GMT
Hi *,

Several jdbc drivers seem to handle trailing spaces on
CHAR fields differently.

Suppose you have a CHAR(20) field in a table, and you put
a string with length < 20 into this field, using
a prepared statement, and the setString() method.

Now, when doing a select, again with a prepared statement,
and reading the string with getString(), one database (SOLID) gives
me "thestring", with no trailing spaces, the other (Daffodil)
returns "thestring           ", with a number of spaces appended.

Which one is right?

Moreover, can I define how to fill the remaining space in
the CHAR field (with spaces or NULLs)?

Thanks,
Benjamin
Joseph Weinstein - 25 Aug 2003 21:59 GMT
> Hi *,
>
[quoted text clipped - 14 lines]
> Moreover, can I define how to fill the remaining space in
> the CHAR field (with spaces or NULLs)?

This is a vaguery of fixed-length character columns. You should use the
more intuitive varchar column, if the DBMS offers it.
  However, assuming you define a fixed-length character column, it is
you who should determine the full contents, in order to avoid ambiguity.
You should therefore pre-pad the value with blanks, or any other character
you want, and the DBMS will accept.
   However, if the DBMS chooses to automatically pad-out any unspecified
characters, nothing is 'correct', it is just a DBMS-specific option in handling
an ambiguity between your column definition and your submitted data. It would
only be a bug if you inserted a value like "the string        " and you got
back "the string".

Joe Weinstein at BEA

> Thanks,
> Benjamin
Ed Yu - 26 Aug 2003 02:16 GMT
The behavior of CHAR data type is fixed width with trailing spaces for ANSI
compliance databases. I belive that there is not a general way to change
this behavior. If you need variable length character field, use varchar.

> Hi *,
>
[quoted text clipped - 17 lines]
> Thanks,
> Benjamin
Real Gagnon - 26 Aug 2003 12:33 GMT
> Moreover, can I define how to fill the remaining space in
> the CHAR field (with spaces or NULLs)?

Some JDBC drivers give you choice about how to handle CHAR() column.

For example, with Oracle it's

oracle.jdbc.driver.OracleConnection c =
(oracle.jdbc.driver.OracleConnection)connection;

c.setDefaultFixedString(true);

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB snippets ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

X_AWemner_X - 26 Aug 2003 14:22 GMT
> Several jdbc drivers seem to handle trailing spaces on
> CHAR fields differently.
[quoted text clipped - 7 lines]
> me "thestring", with no trailing spaces, the other (Daffodil)
> returns "thestring           ", with a number of spaces appended.

It's just an db implementor's choice. But, I would first assume CHAR columns
be padded with trailing spaces when retrieving data from the server. Its
VARCHAR to give non-padded strings. This is what Ive used to see in a
databases I use.

I never use CHAR columns just because a padding might give a side effects.
You should use varchar types if possible and life is easier.
Benjamin Lorenz - 30 Aug 2003 08:46 GMT
> I never use CHAR columns just because a padding might give a side effects.
> You should use varchar types if possible and life is easier.

So, are there any situations in which you really _want_ to use
CHAR columns for a Java-based application?

I learned that performance might be a reason (database fragmentation...).

What are your experiences?

Thanks, again,
Benjamin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.