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.
- 
BINARYexprThe
BINARYoperator converts the expression to a binary string (a string that has thebinarycharacter set andbinarycollation). A common use forBINARYis to force a character string comparison to be done byte by byte using numeric byte values rather than character by character. TheBINARYoperator also causes trailing spaces in comparisons to be significant. For information about the differences between thebinarycollation of thebinarycharacter set and the_bincollations of nonbinary character sets, see Section 10.8.5, “The binary Collation Compared to _bin Collations”.The
BINARYoperator 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 '; -> 0In a comparison,
BINARYaffects 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 exprIf a value is a string literal, it can be designated as a binary string without converting it by using the
_binarycharacter set introducer:mysql> SELECT 'a' = 'A'; -> 1 mysql> SELECT _binary 'a' = 'A'; -> 0For information about introducers, see Section 10.3.8, “Character Set Introducers”.
The
BINARYoperator in expressions differs in effect from theBINARYattribute in character column definitions. For a character column defined with theBINARYattribute, MySQL assigns the table default character set and the binary (_bin) collation of that character set. Every nonbinary character set has a_bincollation. 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_binThe use of
CHARACTER SET binaryin the definition of aCHAR,VARCHAR, orTEXTcolumn 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 BLOBIf
BINARYis 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_valueAT TIME ZONEtimezone_specifierAS DATETIME[(precision)])timezone_specifier: [INTERVAL] '+00:00' | 'UTC'With
CAST(syntax, theexprAStypeCAST()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
typevalues are permitted:- 
BINARY[(N)]Produces a string with the
VARBINARYdata type, except that when the expressionexpris empty (zero length), the result type isBINARY(0). If the optional lengthNis given,BINARY(causes the cast to use no more thanN)Nbytes of the argument. Values shorter thanNbytes are padded with0x00bytes to a length ofN. If the optional lengthNis 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
BINARYaffects comparisons, see Section 11.3.3, “The BINARY and VARBINARY Types”. - 
CHAR[(N)] [charset_info]Produces a string with the
VARCHARdata type. except that when the expressionexpris empty (zero length), the result type isCHAR(0). If the optional lengthNis given,CHAR(causes the cast to use no more thanN)Ncharacters of the argument. No padding occurs for values shorter thanNcharacters. If the optional lengthNis 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_infoclause,CHARproduces a string with the default character set. To specify the character set explicitly, thesecharset_infovalues 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.
 - 
 - 
DATEProduces a
DATEvalue. - 
DATETIME[(M)]Produces a
DATETIMEvalue. If the optionalMvalue is given, it specifies the fractional seconds precision. - 
DECIMAL[(M[,D])]Produces a
DECIMALvalue. If the optionalMandDvalues are given, they specify the maximum number of digits (the precision) and the number of digits following the decimal point (the scale). IfDis omitted, 0 is assumed. IfMis omitted, 10 is assumed. - 
DOUBLEProduces a
DOUBLEresult. Added in MySQL 8.0.17. - 
FLOAT[(p)]If the precision
pis not specified, produces a result of typeFLOAT. Ifpis 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. - 
JSONProduces a
JSONvalue. For details on the rules for conversion of values betweenJSONand 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,NCHARdoes not permit trailing character set information to be specified. - 
REALProduces a result of type
REAL. This is actuallyFLOATif theREAL_AS_FLOATSQL mode is enabled; otherwise the result is of typeDOUBLE. - 
SIGNED [INTEGER]Produces a signed
BIGINTvalue. - 
spatial_typeAs 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
TIMEvalue. If the optionalMvalue is given, it specifies the fractional seconds precision. - 
UNSIGNED [INTEGER]Produces an unsigned
BIGINTvalue. - 
YEARProduces a
YEARvalue. 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
YEARvalue. - 
For a number consisting of one or two digits, or for a string which can be interpeted as such a number, return a
YEARvalue 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, orTIMESTAMPvalue, return theYEARportion of the value. For aTIMEvalue, return the current year.If you do not specify the type of a
TIMEargument, 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 toYEARusing 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); +---------------------+------------------ 
 - 
 
 - 
 
                    
                