You are on page 1of 1

Chapter 1: Creating 9

There are a few exceptions to the assumption a string is a string. First, sorting
and comparisons involving BINARY columns always use the actual binary val-
ues, whereas CHARACTER columns are sorted and compared according to the
database collation sequence and case sensitivity. For example, in a case-insensi-
tive database (the default) the CHARACTER values 'a' and 'A' are treated as
being equal, whereas the BINARY 'a' is treated as being less than the BINARY
'A' when they are compared or sorted.

Tip: Use the CAST function when you need to perform case-sensitive compar-
isons in a case-insensitive database; e.g., IF CAST ( char1 AS BINARY ) = CAST
( char2 AS BINARY ). This also works in the WHERE and ORDER BY clauses, and
can be used when you need to ignore the database collation sequence.

Note: This book doesnt discuss multi-byte character sets, except to note that
some techniques, like the Tip above, are only intended for single-byte character
sets.

Second, a few functions only work on the first 255 bytes of the character string
arguments: SOUNDEX, SIMILAR, and all the date and time functions ignore
anything past 255 bytes.
Third, a conversion from string to numeric will also ignore any data past
255 bytes.
Fourth, an attempt to assign a long string value to a column declared with a
shorter maximum length will result in right truncation. This truncation will hap-
pen silently when only spaces are being truncated by an INSERT or UPDATE
command, or when non-spaces are truncated but the STRING_RTRUNCATION
option is still set to the default 'OFF'. To generate an error message when
non-spaces are truncated you must set STRING_RTRUNCATION to 'ON'. Note
that trailing spaces are significant, and are never truncated unless they wont fit
in the declared maximum length.

Tip: The LTRIM, RTRIM, and TRIM functions can be used to get rid of leading
and trailing spaces.

Fifth, some application development tools generate different code and user
interface elements depending on how a string is declared. In some cases a col-
umn declared as CHAR may be treated as a fixed-length string even though
SQL Anywhere 9 does not implement it that way.

Note: Other database products may implement CHAR columns as


fixed-length strings, and that might affect you if youre sending data back and
forth via proxy tables or MobiLink synchronization.

Finally, there are some performance implications to declaring a string column


with a maximum length far larger than you need. The declared width of col-
umns in an index is used to determine if a compressed B-tree index can be used
instead of a hash B-tree index. Subquery and function caching may be turned
off in cases where the total declared maximum length of the columns and argu-
ments is very large. Also, the query optimizer may be able to get better

You might also like