]> begriffs open source - ai-pg/blob - full-docs/txt/datatype-bit.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / datatype-bit.txt
1
2 8.10. Bit String Types #
3
4    Bit strings are strings of 1's and 0's. They can be used to store or
5    visualize bit masks. There are two SQL bit types: bit(n) and bit
6    varying(n), where n is a positive integer.
7
8    bit type data must match the length n exactly; it is an error to
9    attempt to store shorter or longer bit strings. bit varying data is of
10    variable length up to the maximum length n; longer strings will be
11    rejected. Writing bit without a length is equivalent to bit(1), while
12    bit varying without a length specification means unlimited length.
13
14 Note
15
16    If one explicitly casts a bit-string value to bit(n), it will be
17    truncated or zero-padded on the right to be exactly n bits, without
18    raising an error. Similarly, if one explicitly casts a bit-string value
19    to bit varying(n), it will be truncated on the right if it is more than
20    n bits.
21
22    Refer to Section 4.1.2.5 for information about the syntax of bit string
23    constants. Bit-logical operators and string manipulation functions are
24    available; see Section 9.6.
25
26    Example 8.3. Using the Bit String Types
27 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
28 INSERT INTO test VALUES (B'101', B'00');
29 INSERT INTO test VALUES (B'10', B'101');
30
31 ERROR:  bit string length 2 does not match type bit(3)
32
33 INSERT INTO test VALUES (B'10'::bit(3), B'101');
34 SELECT * FROM test;
35
36   a  |  b
37 -----+-----
38  101 | 00
39  100 | 101
40
41
42    A bit string value requires 1 byte for each group of 8 bits, plus 5 or
43    8 bytes overhead depending on the length of the string (but long values
44    may be compressed or moved out-of-line, as explained in Section 8.3 for
45    character strings).