Search code examples
sap-asejtds

How to detect string truncation


I use jTDS 1.2.6 to connect Adaptive Server Enterprise/15.7.0. When a prepared statement insert rows in a table with a VARCHAR(16) column, the strings are silently truncated. The insertion is successful, no warning and no DataTruncation exception is raised whereas the encoded is string is truncated to 16 octet.

How could I detect the string truncation ?


Solution

  • Adaptive Server truncates entries to the specified column length without warning or error, unless you set string_rtruncation on. See the Reference Manual: Commands.

    MSSQL always detect truncation of strings and the option STRING_RTRUNCATION does not exist. Here is an excerpt of jTDS SAfeTest.testDataTruncException()

    Statement stmt = con.createStatement();
    if (!con.getMetaData().getDatabaseProductName().toLowerCase().startsWith("microsoft")) {
        // By default Sybase will silently truncate strings,
        // set an option to ensure that an exception is thrown.
         stmt.execute("SET STRING_RTRUNCATION ON");
    }