VARBINARY

The VARBINARY function returns a VARBINARY (varying-length binary string) representation of a string of any type.

>>-VARBINARY(string-expression-+------------+-)----------------><
                               '-,--integer-'     

The schema is SYSIBM.

string-expression
An expression that returns a value that is a built-in character string, graphic string, binary string, or a row ID type.
integer
An integer value that specifies the length attribute of the resulting binary string. The value must be an integer between 1 and 32704 inclusive. If integer is not specified:
  • If the string-expression is the empty string constant, the length attribute of the result is 1.
  • Otherwise, the length attribute of the result is the same as the length attribute of the string-expression, unless the string-expression is a graphic string. In this case, the length attribute of the result is twice the length attribute of the string-expression.

The result of the function is a varying-length binary string.

The result can be null; if the first argument is null, the result is the null value.

The actual length of the result is the minimum of the length attribute of the result and the actual length of the string-expression (or twice the length of the string-expression if string-expression returns a graphic string). If the length of the string-expression is greater than the length attribute of the result, truncation is performed, and a warning is returned unless the string-expression is a character string and all the truncated characters are blanks, or the string-expression is a graphic string and all the truncated characters are double-byte blanks.

Example 1: The following function returns a varying-length binary string with a length attribute 1, actual length 0, and a value of empty string:
   SELECT VARBINARY('') 
     FROM SYSIBM.SYSDUMMY1;
Example 2: The following function returns a varying-length binary string with a length attribute 5, actual length 3, and a value BX'D2C2C8':
  SELECT VARBINARY('KBH',5) 
    FROM SYSIBM.SYSDUMMY1;
Example 3: The following function returns a varying-length binary string with a length attribute 3, actual length 3, and a value BX'D2C2C8'
  SELECT VARBINARY('KBH ',3) 
    FROM SYSIBM.SYSDUMMY1;
Example 4: The following function returns a varying-length binary string with a length attribute 3, actual length 3, and a value BX'D2C2C8', a warning is also returned.
  SELECT VARBINARY('KBH-93',3) 
    FROM SYSIBM.SYSDUMMY1;
Example 5: The following function returns a varying-length binary string with a length attribute 3, actual length 3, and a value BX'C1C2C3', a warning is also returned.
  SELECT VARBINARY(BINARY('ABC',5),3) 
    FROM SYSIBM.SYSDUMMY1;