Bit String Types
Bit strings are strings of 1's and 0's. They can be used to store bit masks.
GaussDB supports two bit string types: bit(n) and bit varying(n). Here, n is a positive integer. The maximum value of n is 83886080, which is equivalent to 10 MB.
The bit type data must match the length n exactly. An error will be reported if the data length of the storage is not matched. Data of the bit varying type is of variable length up to the maximum length n. Longer strings will be rejected. Writing bit without a length is equivalent to bit(1), while bit varying without a length specification means unlimited length.
- If you convert a bit string value to bit(n), the content that exceeds n digits on the right of the bit string value will be truncated, or the bit string value will be padded with zeros to n digits on the right if the bit string value is less than n digits. In this case, no error message is displayed.
- Similarly, if one explicitly casts a bit-string value to bit varying(n), the bit-string value will be truncated on the right if it is more than n bits.
- When the ADMS platform driver version 8.1.3-200 or earlier is used, use ::bit varying to convert the bit type. Otherwise, an error may occur.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
-- Create a table. gaussdb=# CREATE TABLE bit_type_t1 ( BT_COL1 INTEGER, BT_COL2 BIT(3), BT_COL3 BIT VARYING(5) ) DISTRIBUTE BY REPLICATION; -- Insert data. gaussdb=# INSERT INTO bit_type_t1 VALUES(1, B'101', B'00'); -- Specify the type length. An error is reported if an inserted string exceeds this length. gaussdb=# INSERT INTO bit_type_t1 VALUES(2, B'10', B'101'); ERROR: bit string length 2 does not match type bit(3) CONTEXT: referenced column: bt_col2 -- Specify the type length. Data is converted if it exceeds this length. gaussdb=# INSERT INTO bit_type_t1 VALUES(2, B'10'::bit(3), B'101'); -- View data. gaussdb=# SELECT * FROM bit_type_t1; bt_col1 | bt_col2 | bt_col3 ---------+---------+--------- 1 | 101 | 00 2 | 100 | 101 (2 rows) -- Drop the table. gaussdb=# DROP TABLE bit_type_t1; |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot