Search code examples
db2db2-luw

How to define VARCHAR columns to support special characters?


In my table product I have a column: product_name with type VARCHAR and size of 100: product_name varchar(100)

When I try to insert a name with special characters like this one:

°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%°°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°%âä°

I get this error:

ERROR : org.hibernate.util.JDBCExceptionReporter:78 : logExceptions() : Error for batch element #1: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.57.82

My product_name column can have 100 characters, for me 'â' is 1 character.

Is there in DB2 another type (other than varchar), to set it for the product_name column?

thus I can execute this query:

alter table product alter column product_name set data type otherType(100);

Solution

  • Check out the

    STRING_UNITS
    

    database configuration parameter. You can use it to switch fronm the default byte length to a character length. This mean char(100) will be default interpreted by DB2 as 100 Bytes. As chracter in a unicode database can span 1-4 bytes 100 bytes are not enough to store 100 chars. After switching to STRING_UNITS = CODEUNITS32 you get 100 chars when defining a column varchar(100). So you do not need another type but another db cfg setting.