For storing raw binary data, such as pictures, in a hexadecimal-like notation, up to a length of (32K - 1) bytes.
BINARY [ ( length ) ]
VARBINARY [ ( max-length ) ]
Binary data begins with the characters “0x” or “0X” and can include any combination of digits and the uppercase and lowercase letters A through F. You can specify the column length in bytes, or use the default length of 1 byte. Each byte stores 2 hexadecimal digits. Even though the default length is 1 byte, always specifying an even number of characters for BINARY and VARBINARY column length is recommended. If you enter a value longer than the specified column length, Sybase IQ truncates the entry to the specified length without warning or error.
BINARY Binary data of length length bytes. If length is omitted, the default is 1 byte. The maximum size allowed is 255. Use the fixed-length binary type BINARY for data in which all entries are expected to be approximately equal in length. Because entries in BINARY columns are zero-padded to the column length length, they may require more storage space than entries in VARBINARY columns.
VARBINARY Binary data up to a length of max-length bytes. If max-length is omitted, the default is 1 byte. The maximum size allowed is (32K - 1) bytes. Use the variable-length binary type VARBINARY for data that is expected to vary greatly in length.
As a separately licensed option, Sybase IQ supports Binary Large Object (BLOB) data with a length ranging from zero (0) to 512TB (terabytes) for an IQ page size of 128KB or 2PB (petabytes) for an IQ page size of 512KB. (The maximum length is equal to 4GB multiplied by the database page size.) For more information, see Large Objects Management in Sybase IQ.
Treatment of trailing zeros All BINARY columns are padded with zeros to the full width of the column. Trailing zeros are truncated in all VARBINARY columns.
The following example creates a table with all four variations of BINARY and VARBINARY data types defined with NULL and NOT NULL. The same data is inserted in all four columns and is padded or truncated according to the data type of the column.
CREATE TABLE zeros (bnot BINARY(5) NOT NULL, bnull BINARY(5) NULL, vbnot VARBINARY(5) NOT NULL, vbnull VARBINARY(5) NULL); INSERT zeros VALUES (0x12345000, 0x12345000, 0x12345000, 0x12345000); INSERT zeros VALUES (0x123, 0x123, 0x123, 0x123); INSERT zeros VALUES (0x0, 0x0, 0x0, 0x0); INSERT zeros VALUES ('002710000000ae1b', '002710000000ae1b', '002710000000ae1b', '002710000000ae1b'); SELECT * FROM zeros;
bnot |
bnull |
vbnot |
vbnull |
---|---|---|---|
0x1234500000 |
0x1234500000 |
0x12345000 |
0x12345000 |
0x0123000000 |
0x0123000000 |
0x0123 |
0x0123 |
0x0000000000 |
0x0000000000 |
0x00 |
0x00 |
0x3030323731 |
0x3030323731 |
0x3030323731 |
0x3030323731 |
Because each byte of storage holds 2 hexadecimal digits, Sybase IQ expects binary entries to consist of the characters “0x” followed by an even number of digits. When the “0x” is followed by an odd number of digits, IQ assumes that you omitted the leading 0 and adds it for you.
Input values “0x00” and “0x0” are stored as “0x00” in variable-length binary columns (VARBINARY). In fixed-length binary columns (BINARY), the value is padded with zeros to the full length of the field:
INSERT zeros VALUES (0x0, 0x0, 0x0, 0x0); SELECT * FROM zeros WHERE bnot = 0x00;
bnot |
bnull |
vbnot |
vbnull |
---|---|---|---|
0x0000000000 |
0x0000000000 |
0x00 |
0x00 |
If the input value does not include the “0x”, Sybase IQ assumes that the value is an ASCII value and converts it. For example:
CREATE TABLE sample (col_bin BINARY(8)); INSERT sample VALUES ('002710000000ae1b'); SELECT * FROM sample;
col_bin |
---|
0x3030323731303030 |
Loading ASCII data from a flat file Any ASCII data loaded from a flat file into a binary type column (BINARY or VARBINARY) is stored as nibbles. For example, if 0x1234 or 1234 is read from a flat file into a binary column, IQ stores the value as hexadecimal 1234. IQ ignores the “0x” prefix. If the input data contains any characters out of the range 0 - 9, a - f, and A - F, the data is rejected.
Storage size Table 4-3 lists the storage size of binary data.
Data type |
Column definition |
Input data |
Storage |
---|---|---|---|
VARBINARY |
width of (32K - 1) bytes |
(32K - 1) bytes binary |
(32K - 1) bytes |
VARBINARY |
width of (32K - 1) bytes |
(64K - 2) bytes ASCII |
(32K - 1) bytes |
BINARY |
width of 255 bytes |
255 bytes binary |
255 bytes |
BINARY |
width of 255 byte |
510 bytes ASCII |
255 bytes |
Platform dependence The exact form in which you enter a particular value depends on the platform you are using. Therefore, calculations involving binary data can produce different results on different machines.
For platform-independent conversions between hexadecimal strings and integers, use the INTTOHEX and HEXTOINT functions rather than the platform-specific CONVERT function. For details, see the section “Data type conversion functions”.
String operators The concatenation string operators || and + both support binary type data. Explicit conversion of binary operands to character data types is not necessary with the || operator. Explicit and implicit data conversion produce different results, however.
The following restrictions apply to columns containing BINARY and VARBINARY data:
You cannot use the aggregate functions SUM, AVG, STDDEV, or VARIANCE with the binary data types. The aggregate functions MIN, MAX, and COUNT do support the binary data types BINARY and VARBINARY.
HNG, WD, DATE, TIME, and DTTM indexes do not support BINARY or VARBINARY data.
Only the default index and CMP index types are supported for VARBINARY data greater than 255 bytes in length.
Bit operations are supported on BINARY and VARBINARY data that is 8 bytes or less in length.
The treatment of trailing zeros in binary data types is the same in Adaptive Server Anywhere and Sybase IQ, but is different in Adaptive Server Enterprise. Table 4-4 shows the differences.
Data type |
ASA and IQ |
ASE |
---|---|---|
BINARY NOT NULL |
padded |
padded |
BINARY NULL |
padded |
truncated |
VARBINARY NOT NULL |
not padded, not truncated |
truncated |
VARBINARY NULL |
not padded, not truncated |
truncated |
Adaptive Server Enterprise, Adaptive Server Anywhere, and Sybase IQ all support the STRING_RTRUNCATION database option, which affects error message reporting when an INSERT or UPDATE string is truncated. For Transact-SQL compatible string comparisons, set the STRING_RTRUNCATION option to the same value in both databases.
You can also set the STRING_RTRUNCATION option ON when loading data into a table, to alert you that the data is too large to load into the field. The default value is OFF.
Bit operations on binary type data are not supported by ASE. ASA only supports bit operations against the first four bytes of binary type data. IQ supports bit operations against the first 8 bytes of binary type data.