Cast a value as a certain type
小结:
1)获取当日凌晨时间戳
MySQL :: MySQL 8.0 Reference Manual :: 12.11 Cast Functions and Operators https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html
12.11 Cast Functions and Operators
Cast functions and operators enable conversion of values from one data type to another.
-
BINARY
expr
The
BINARY
operator converts the expression to a binary string (a string that has thebinary
character set andbinary
collation). A common use forBINARY
is to force a character string comparison to be done byte by byte using numeric byte values rather than character by character. TheBINARY
operator also causes trailing spaces in comparisons to be significant. For information about the differences between thebinary
collation of thebinary
character set and the_bin
collations of nonbinary character sets, see Section 10.8.5, “The binary Collation Compared to _bin Collations”.The
BINARY
operator is deprecated as of MySQL 8.0.27, and you should expect its removal in a future version of MySQL. UseCAST(... AS BINARY)
instead.mysql> SELECT 'a' = 'A'; -> 1 mysql> SELECT BINARY 'a' = 'A'; -> 0 mysql> SELECT 'a' = 'a '; -> 1 mysql> SELECT BINARY 'a' = 'a '; -> 0
In a comparison,
BINARY
affects the entire operation; it can be given before either operand with the same result.To convert a string expression to a binary string, these constructs are equivalent:
CONVERT(expr USING BINARY) CAST(expr AS BINARY) BINARY expr
If a value is a string literal, it can be designated as a binary string without converting it by using the
_binary
character set introducer:mysql> SELECT 'a' = 'A'; -> 1 mysql> SELECT _binary 'a' = 'A'; -> 0
For information about introducers, see Section 10.3.8, “Character Set Introducers”.
The
BINARY
operator in expressions differs in effect from theBINARY
attribute in character column definitions. For a character column defined with theBINARY
attribute, MySQL assigns the table default character set and the binary (_bin
) collation of that character set. Every nonbinary character set has a_bin
collation. For example, if the table default character set isutf8mb4
, these two column definitions are equivalent:CHAR(10) BINARY CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
The use of
CHARACTER SET binary
in the definition of aCHAR
,VARCHAR
, orTEXT
column causes the column to be treated as the corresponding binary string data type. For example, the following pairs of definitions are equivalent:CHAR(10) CHARACTER SET binary BINARY(10) VARCHAR(10) CHARACTER SET binary VARBINARY(10) TEXT CHARACTER SET binary BLOB
If
BINARY
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the--binary-as-hex
. For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”. -
CAST(
timestamp_value
AT TIME ZONEtimezone_specifier
AS DATETIME[(precision
)])timezone_specifier
: [INTERVAL] '+00:00' | 'UTC'With
CAST(
syntax, theexpr
AStype
CAST()
function takes an expression of any type and produces a result value of the specified type. This operation may also be expressed asCONVERT(
, which is equivalent.expr
,type
)These
type
values are permitted:-
BINARY[(
N
)]Produces a string with the
VARBINARY
data type, except that when the expressionexpr
is empty (zero length), the result type isBINARY(0)
. If the optional lengthN
is given,BINARY(
causes the cast to use no more thanN
)N
bytes of the argument. Values shorter thanN
bytes are padded with0x00
bytes to a length ofN
. If the optional lengthN
is not given, MySQL calculates the maximum length from the expression. If the supplied or calculated length is greater than an internal threshold, the result type isBLOB
. If the length is still too long, the result type isLONGBLOB
.For a description of how casting to
BINARY
affects comparisons, see Section 11.3.3, “The BINARY and VARBINARY Types”. -
CHAR[(
N
)] [charset_info
]Produces a string with the
VARCHAR
data type. except that when the expressionexpr
is empty (zero length), the result type isCHAR(0)
. If the optional lengthN
is given,CHAR(
causes the cast to use no more thanN
)N
characters of the argument. No padding occurs for values shorter thanN
characters. If the optional lengthN
is not given, MySQL calculates the maximum length from the expression. If the supplied or calculated length is greater than an internal threshold, the result type isTEXT
. If the length is still too long, the result type isLONGTEXT
.With no
charset_info
clause,CHAR
produces a string with the default character set. To specify the character set explicitly, thesecharset_info
values are permitted:-
CHARACTER SET
: Produces a string with the given character set.charset_name
-
ASCII
: Shorthand forCHARACTER SET latin1
. -
UNICODE
: Shorthand forCHARACTER SET ucs2
.
In all cases, the string has the character set default collation.
-
-
DATE
Produces a
DATE
value. -
DATETIME[(
M
)]Produces a
DATETIME
value. If the optionalM
value is given, it specifies the fractional seconds precision. -
DECIMAL[(
M
[,D
])]Produces a
DECIMAL
value. If the optionalM
andD
values are given, they specify the maximum number of digits (the precision) and the number of digits following the decimal point (the scale). IfD
is omitted, 0 is assumed. IfM
is omitted, 10 is assumed. -
DOUBLE
Produces a
DOUBLE
result. Added in MySQL 8.0.17. -
FLOAT[(
p
)]If the precision
p
is not specified, produces a result of typeFLOAT
. Ifp
is provided and 0 <= <p
<= 24, the result is of typeFLOAT
. If 25 <=p
<= 53, the result is of typeDOUBLE
. Ifp
< 0 orp
> 53, an error is returned. Added in MySQL 8.0.17. -
JSON
Produces a
JSON
value. For details on the rules for conversion of values betweenJSON
and other types, see Comparison and Ordering of JSON Values. -
NCHAR[(
N
)]Like
CHAR
, but produces a string with the national character set. See Section 10.3.7, “The National Character Set”.Unlike
CHAR
,NCHAR
does not permit trailing character set information to be specified. -
REAL
Produces a result of type
REAL
. This is actuallyFLOAT
if theREAL_AS_FLOAT
SQL mode is enabled; otherwise the result is of typeDOUBLE
. -
SIGNED [INTEGER]
Produces a signed
BIGINT
value. -
spatial_type
As of MySQL 8.0.24,
CAST()
andCONVERT()
support casting geometry values from one spatial type to another, for certain combinations of spatial types. For details, see Cast Operations on Spatial Types. -
TIME[(
M
)]Produces a
TIME
value. If the optionalM
value is given, it specifies the fractional seconds precision. -
UNSIGNED [INTEGER]
Produces an unsigned
BIGINT
value. -
YEAR
Produces a
YEAR
value. Added in MySQL 8.0.22. These rules govern conversion toYEAR
:-
For a four-digit number in the range 1901-2155 inclusive, or for a string which can be interpreted as a four-digit number in this range, return the corresponding
YEAR
value. -
For a number consisting of one or two digits, or for a string which can be interpeted as such a number, return a
YEAR
value as follows:-
If the number is in the range 1-69 inclusive, add 2000 and return the sum.
-
If the number is in the range 70-99 inclusive, add 1900 and return the sum.
-
-
For a string which evaluates to 0, return 2000.
-
For the number 0, return 0.
-
For a
DATE
,DATETIME
, orTIMESTAMP
value, return theYEAR
portion of the value. For aTIME
value, return the current year.If you do not specify the type of a
TIME
argument, you may get a different result from what you expect, as shown here:mysql> SELECT CAST("11:35:00" AS YEAR), CAST(TIME "11:35:00" AS YEAR); +--------------------------+-------------------------------+ | CAST("11:35:00" AS YEAR) | CAST(TIME "11:35:00" AS YEAR) | +--------------------------+-------------------------------+ | 2011 | 2021 | +--------------------------+-------------------------------+
-
If the argument is of type
DECIMAL
,DOUBLE
,DECIMAL
, orREAL
, round the value to the nearest integer, then attempt to cast the value toYEAR
using the rules for integer values, as shown here:mysql> SELECT CAST(1944.35 AS YEAR), CAST(1944.50 AS YEAR); +-----------------------+-----------------------+ | CAST(1944.35 AS YEAR) | CAST(1944.50 AS YEAR) | +-----------------------+-----------------------+ | 1944 | 1945 | +-----------------------+-----------------------+ mysql> SELECT CAST(66.35 AS YEAR), CAST(66.50 AS YEAR); +---------------------+------------------
-
-