Bit Functions
950
SELECT
enum_col
FROM
tbl_name
ORDER BY CAST(
enum_col
AS CHAR);
CAST(str AS BINARY)
[948]
is the same thing as
BINARY str
[948]
.
CAST(expr AS
CHAR)
[948]
treats the expression as a string with the default character set.
CAST()
[948]
also changes the result if you use it as part of a more complex expression such as
CONCAT('Date: ',CAST(NOW() AS DATE))
[887]
.
You should not use
CAST()
[948]
to extract data in different formats but instead use string functions
like
LEFT()
[890]
or
EXTRACT()
[923]
. See
Section 12.7, “Date and Time Functions”
.
To cast a string to a numeric value in numeric context, you normally do not have to do anything other
than to use the string value as though it were a number:
mysql>
SELECT 1+'1';
-> 2
If you use a string in an arithmetic operation, it is converted to a floating-point number during
expression evaluation.
If you use a number in string context, the number automatically is converted to a string:
mysql>
SELECT CONCAT('hello you ',2);
-> 'hello you 2'
For information about implicit conversion of numbers to strings, see
Section 12.2, “Type Conversion in
Expression Evaluation”
.
MySQL supports arithmetic with both signed and unsigned 64-bit values. If you are using numeric
operators (such as
+
[906]
or
-
[906]
) and one of the operands is an unsigned integer, the result
is unsigned by default (see
Section 12.6.1, “Arithmetic Operators”
). You can override this by using the
SIGNED
or
UNSIGNED
cast operator to cast a value to a signed or unsigned 64-bit integer, respectively.
mysql>
SELECT CAST(1-2 AS UNSIGNED)
-> 18446744073709551615
mysql>
SELECT CAST(CAST(1-2 AS UNSIGNED) AS SIGNED);
-> -1
If either operand is a floating-point value, the result is a floating-point value and is not affected by the
preceding rule. (In this context,
DECIMAL
column values are regarded as floating-point values.)
mysql>
SELECT CAST(1 AS UNSIGNED) - 2.0;
-> -1.0
The SQL mode affects the result of conversion operations. Examples:
• If you convert a “zero” date string to a date,
CONVERT()
[948]
and
CAST()
[948]
return
NULL
when the
NO_ZERO_DATE
[538]
SQL mode is enabled. As of MySQL 5.0.4, they also produce a
warning.
• For integer subtraction, if the
NO_UNSIGNED_SUBTRACTION
[537]
SQL mode is enabled, the
subtraction result is signed even if any operand is unsigned.
For more information, see
Section 5.1.7, “Server SQL Modes”
.
12.11. Bit Functions
Table 12.15. Bitwise Functions
Name
Description
BIT_COUNT()
[952]
Return the number of bits that are set
&
[951]
Bitwise AND
~
[951]
Invert bits
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...