Protect Ciphertext from Accidental Truncation

To prevent accidental truncation of the ciphertext output of the encrypt function (or accidental truncation of any other character or binary string), set the STRING_RTRUNCATION database option.

SET OPTION STRING_RTRUNCATION = 'ON'

When STRING_RTRUNCATION is ON (the default), the engine raises an error whenever a string would be truncated during a load, insert, update, or SELECT INTO operation. This is ISO/ANSI SQL behavior and is a recommended practice.

When explicit truncation is required, use a string expression such as LEFT, SUBSTRING, or CAST.

Setting STRING_RTRUNCATION OFF forces silent truncation of strings.

The AES_DECRYPT function also checks input ciphertext for valid data length, and checks text output to verify both the resulting data length and the correctness of the supplied key. If you supply the data type argument, the data type is checked as well.