Binary Operators

Each of the functions in the following table works with BINARY and VARBINARY data types.

Operator Function Description
'='  
binary_eq

Equal to

'<>'
binary_ne

Not equal to

'<'
binary_lt

Less than

'<='
binary_le

Less than or equal to

'>'
binary_gt

Greater than

 '>='
binary_ge

Greater than or equal to

'&'
binary_and

And

'~'
binary_not

Not

'|'
binary_or

Or

'#'
binary_xor

Either or

'||'
binary_cat

Concatenate

Notes

Similarly, to apply the LENGTH, REPEAT, TO_HEX, and SUBSTRING functions to a BINARY type, explicitly cast the argument; for example:

=> SELECT LENGTH('\\001\\002\\003\\004'::varbinary(4)); 
LENGTH
--------
      4
(1 row)

When applying an operator or function to a column, the operator's or function's argument type is derived from the column type.

Examples

In the following example, the zero byte is not removed from column cat1 when values are concatenated:

=> SELECT 'ab'::BINARY(3) || 'cd'::BINARY(2) AS cat1, 
'ab'::VARBINARY(3) ||
    'cd'::VARBINARY(2) AS cat2;
   cat1   | cat2
----------+------
 ab\000cd | abcd
(1 row)

When the binary value 'ab'::binary(3) is translated to varbinary, the result is equivalent to 'ab\\000'::varbinary(3); for example:

=> SELECT 'ab'::binary(3); binary
--------
 ab\000
(1 row)

The following example performs a bitwise AND operation on the two input values (see also BIT_AND):

=> SELECT '10001' & '011' as AND; 
 AND
-----
   1
(1 row)

The following example performs a bitwise OR operation on the two input values (see also BIT_OR):

=> SELECT '10001' | '011' as OR;  
  OR
-------
 10011
(1 row)

The following example concatenates the two input values:

=> SELECT '10001' || '011' as CAT;   
   CAT
----------
 10001011
(1 row)