OpenESQL Fixed-length Character Strings Handling

Typically, COBOL PIC X(n) host variables are not terminated with the null character ('\0'), but rather with trailing spaces (see Fixed-length Character Strings for details). When using PIC X(n) host variables with VARCHAR columns or within certain SQL expressions, it is difficult to determine whether to present the trailing spaces to the database or not.

To resolve this, OpenESQL strips all trailing spaces by default before presenting an input host variable value to the database. This approach works well, as the database itself pads host variables with trailing spaces before placing them into CHAR columns, and does not pad VARCHAR columns with trailing spaces. However, if you need your database to preserve space padding in a VARCHAR column, the best practice is to use a variable-length character string (see Variable-length Character Strings for details), which eliminates all ambiguity. In fact, when using VARCHAR columns at all, a variable-length character string is always the best fit.

Unfortunately, when using PIC X(n) host variables with SQL expressions, the database decides whether to pad or not to pad the host variable contents.

For example, when using SQL Server, you might experience problems with fixed-length character strings used with LIKE predicates. In these cases, set the PICXBINDING SQL compiler directive option to VARIABLE (e.g., SQL(PICXBINDING=VARIABLE)). This ensures that SQL Server does not pad PIC X(n) host variables, and that LIKE expressions behave as expected. See the PICXBINDING reference topic for complete information on using this SQL compiler directive option.

Tip: In general, it is best to write SQL statements in a portable manner and use RTRIM(:hostvariable) to suppress trailing spaces, and CAST(:hostvariable as CHAR(n)) to preserve trailing spaces. When dealing with legacy code that is not easily modified, rely on OpenESQL to handle trailing spaces, with or without PICXBINDING.