Search code examples
databasenetbeansderby

Derby DB adding white-spaces to CHAR values


I'm building a desktop application using derby Database... While inserting data into a table which contains several columns having data type "CHAR (10)", the database adds white-spaces to the value. For ex. I'm adding a name "Derby" which is of 5 alphabets, the remaining 5 alphabets are added as white-space. The problem starts when I retrieve these values from DB, those whit-spaces are also retrieved along with the actual value. This doesn't happens with the "VARCHAR" data type. How to avoid this?


Solution

  • That's how the CHAR data type is defined in the SQL standard. Derby just follows those rules as documented in the manual (and essentially every DBMS complies with these rules, in some you can disable this however)

    There is nothing you can do to "avoid" this - just use VARCHAR if you don't want that padding.

    In my personal opinion: the CHAR data type is essentially useless nowadays. In almost every DBMS VARCHAR is more efficient (as it requires less storage) and does not suffer from this required but annoying padding behavior.