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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>8.9.Bit String Types</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="datatype.html" title="Chapter8.Data Types">
<link rel="prev" href="datatype-net-types.html" title="8.8.Network Address Types">
<link rel="next" href="arrays.html" title="8.10.Arrays">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="datatype-bit"></a>8.9.Bit String Types</h2></div></div></div>
<a name="id591089"></a><p> Bit strings are strings of 1's and 0's. They can be used to store
or visualize bit masks. There are two SQL bit types:
<code class="type">bit(<em class="replaceable"><code>n</code></em>)</code> and <code class="type">bit
varying(<em class="replaceable"><code>n</code></em>)</code>, where
<em class="replaceable"><code>n</code></em> is a positive integer.
</p>
<p> <code class="type">bit</code> type data must match the length
<em class="replaceable"><code>n</code></em> exactly; it is an error to attempt to
store shorter or longer bit strings. <code class="type">bit varying</code> data is
of variable length up to the maximum length
<em class="replaceable"><code>n</code></em>; longer strings will be rejected.
Writing <code class="type">bit</code> without a length is equivalent to
<code class="literal">bit(1)</code>, while <code class="type">bit varying</code> without a length
specification means unlimited length.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> If one explicitly casts a bit-string value to
<code class="type">bit(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated or
zero-padded on the right to be exactly <em class="replaceable"><code>n</code></em> bits,
without raising an error. Similarly,
if one explicitly casts a bit-string value to
<code class="type">bit varying(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated
on the right if it is more than <em class="replaceable"><code>n</code></em> bits.
</p>
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> Prior to <span class="productname">PostgreSQL</span> 7.2, <code class="type">bit</code> data
was always silently truncated or zero-padded on the right, with
or without an explicit cast. This was changed to comply with the
<acronym class="acronym">SQL</acronym> standard.
</p>
</div>
<p> Refer to <a href="sql-syntax.html#sql-syntax-bit-strings" title="4.1.2.3.Bit-String Constants">Section4.1.2.3, “Bit-String Constants”</a> for information about the syntax
of bit string constants. Bit-logical operators and string
manipulation functions are available; see <a href="functions-bitstring.html" title="9.6.Bit String Functions and Operators">Section9.6, “Bit String Functions and Operators”</a>.
</p>
<div class="example">
<a name="id591240"></a><p class="title"><b>Example8.3.Using the bit string types</b></p>
<div class="example-contents"><pre class="programlisting">CREATE TABLE test (a BIT(3), b BIT VARYING(5));
INSERT INTO test VALUES (B'101', B'00');
INSERT INTO test VALUES (B'10', B'101');
<code class="computeroutput">ERROR: bit string length 2 does not match type bit(3)</code>
INSERT INTO test VALUES (B'10'::bit(3), B'101');
SELECT * FROM test;
<code class="computeroutput"> a | b
-----+-----
101 | 00
100 | 101</code></pre></div>
</div>
<br class="example-break">
</div></body>
</html>
|