The cast function is used to explicitly convert an expression of a certain data type to another data type. The parameter of the cast() function is an expression, which includes the source value and target data type separated by the as keyword.
语法:CAST(expression AS data_type)
expression: any valid SQLService expression
AS: Used to separate two parameters, before AS is the data to be processed, and after AS is the data type to be converted.
data_type: data types provided by the target system, including bigint and sql_variant, user-defined data types cannot be used
The type can be:
CHAR[(N)] Character
type DATE Date type
DATETIME Date and time type
DECIMAL float type
SIGNED int
TIME Time type
E.g:
1. SELECT CAST('9.5' AS decimal(10,2));
It is to convert data 9.5 to decimal number type, 10 is the number of decimal digits that can be stored, 2 is the number of decimal digits that can be stored to the right of the decimal point, the default number is 0. The insufficient number should be added
The result is 9.50
About decimal: https://blog.csdn.net/Poolweet_/article/details/109625426
2. date: 2020-11-11 15:31:26
select cast(date as signed) as date from table1;
Refers to the date converted to SIGNED int type
The result is 20201111153126
3. Data 20201111153126
select cast(date as char) as date from table1;
The result is 2020-11-11 15:31:26