Rechercher dans le manuel MySQL
9.1.5 Bit-Value Literals
Bit-value literals are written using
b'
or
val
'0b
notation.
val
val
is a binary value written using
zeros and ones. Lettercase of any leading b
does not matter. A leading 0b
is case
sensitive and cannot be written as 0B
.
Legal bit-value literals:
- b'01'
- B'01'
- 0b01
Illegal bit-value literals:
b'2' (2 is not a binary digit)
0B01 (0B must be written as 0b)
By default, a bit-value literal is a binary string:
- +------------+---------------------+
- +------------+---------------------+
- +------------+---------------------+
- +-----------+--------------------+
- +-----------+--------------------+
- +-----------+--------------------+
A bit-value literal may have an optional character set
introducer and COLLATE
clause, to designate
it as a string that uses a particular character set and
collation:
Examples:
The examples use
b'
notation,
but val
'0b
notation
permits introducers as well. For information about introducers,
see Section 10.3.8, “Character Set Introducers”.
val
In numeric contexts, MySQL treats a bit literal like an integer.
To ensure numeric treatment of a bit literal, use it in numeric
context. Ways to do this include adding 0 or using
CAST(... AS UNSIGNED)
. For
example, a bit literal assigned to a user-defined variable is a
binary string by default. To assign the value as a number, use
it in numeric context:
- +------+------+------+
- | @v1 | @v2 | @v3 |
- +------+------+------+
- | a | 97 | 97 |
- +------+------+------+
An empty bit value (b''
) evaluates to a
zero-length binary string. Converted to a number, it produces 0:
- +--------------+-------------+
- +--------------+-------------+
- +--------------+-------------+
- +-------+
- | b''+0 |
- +-------+
- | 0 |
- +-------+
Bit-value notation is convenient for specifying values to be
assigned to BIT
columns:
Bit values in result sets are returned as binary values, which
may not display well. To convert a bit value to printable form,
use it in numeric context or use a conversion function such as
BIN()
or
HEX()
. High-order 0 digits are
not displayed in the converted value.
- +------+----------+--------+--------+
- +------+----------+--------+--------+
- | 255 | 11111111 | 377 | FF |
- | 10 | 1010 | 12 | A |
- | 5 | 101 | 5 | 5 |
- +------+----------+--------+--------+
For bit literals, bit operations are considered numeric context,
but bit operations permit numeric or binary string arguments in
MySQL 8.0 and higher. To explicitly specify binary
string context for bit literals, use a
_binary
introducer for at least one of the
arguments:
- +----------+----------+
- +----------+----------+
- | 3F | 003F |
- +----------+----------+
The displayed result appears similar for both bit operations,
but the result without _binary
is a
BIGINT
value, whereas the result with
_binary
is a binary string. Due to the
difference in result types, the displayed values differ:
High-order 0 digits are not displayed for the numeric result.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-bit-value-literals.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.