CAST function In the previous article, we mentioned the CAST function, which is used to display type conversion. There are many benefits when it comes to avoiding implicit type conversions. In fact, there are still many details here that need to be sorted out. First, let’s look at the following conversion: mysql> SELECT CAST('2017-12-14' AS DATE); +----------------------------+ | CAST('2017-12-14' AS DATE) | +----------------------------+ | 2017-12-14 | +----------------------------+ 1 row in set (0.00 sec) in: 2017-12-14 is the data to be converted. DATE is the converted type. The standard syntax is this: CAST(expr AS type) It should be noted here that the type type does not support all data types, but supports specific data types, which is also the focus of today's article. (I have suffered this loss before. I took it for granted that all data types were supported, but I was slapped in the face). Unsupported error: mysql> SELECT CAST('1024' AS int); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int)' at line 1 Supported Types The following is a list of data types that the CAST function supports conversion:
Notice: The supported range of DATE is: 1000-01-01 to 9999-12-31 (experimental version:) If it is: 999-01-01 the result will be 0999-01-01. If it is: 01-01-01 then it will be: 2001-01-01. mysql> select cast('999-11-11' as DATE); +---------------------------+ | cast('999-11-11' as DATE) | +---------------------------+ | 0999-11-11 | +---------------------------+ 1 row in set (0.00 sec) mysql> select cast('01-11-11' as DATE); +--------------------------+ | cast('01-11-11' as DATE) | +--------------------------+ | 2001-11-11 | +--------------------------+ 1 row in set (0.00 sec) mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.20 | +-----------+ 1 row in set (0.00 sec) 2. The value of expr in the CAST function can be converted to type, and the result of the conversion is correct. Otherwise, the result after conversion is the default value, such as Null, 0, etc. For example: when a Char type is converted to a Demical type, the result of the conversion is 0. mysql> SELECT CAST('ANDYQIAN' AS DECIMAL); +-----------------------------+ | CAST('ANDYQIAN' AS DECIMAL) | +-----------------------------+ | 0 | +-----------------------------+ 1 row in set, 1 warning (0.00 sec) Conversion Case Here are some commonly used type conversion examples. DATE Type mysql> select cast('2017-12-14' as DATE); +----------------------------+ | cast('2017-12-14' as DATE) | +----------------------------+ | 2017-12-14 | +----------------------------+ 1 row in set (0.00 sec) TIME type mysql> select cast('12:00:00' as TIME); +--------------------------+ | cast('12:00:00' as TIME) | +--------------------------+ | 12:00:00 | +--------------------------+ 1 row in set (0.00 sec) DATETIM type mysql> select cast('2017-12-14 00:11:11' as DATETIME); +-----------------------------------------+ | cast('2017-12-14 00:11:11' as DATETIME) | +-----------------------------------------+ | 2017-12-14 00:11:11 | +-----------------------------------------+ 1 row in set (0.00 sec) SIGNED Type mysql> select cast('-1024' as SIGNED); +-------------------------+ | cast('-1024' as SIGNED) | +-------------------------+ |-1024 | +-------------------------+ 1 row in set (0.00 sec) UNSIGNED Types mysql> select cast('-1024' as UNSIGNED); +---------------------------+ | cast('-1024' as UNSIGNED) | +---------------------------+ | 18446744073709550592 | +---------------------------+ 1 row in set, 1 warning (0.00 sec) DECIMAL Type mysql> select cast('18.11' as DECIMAL(18,2)); +--------------------------------+ | cast('18.11' as DECIMAL(18,2)) | +--------------------------------+ | 18.11 | +--------------------------------+ 1 row in set (0.00 sec) The above is all the content of this compilation. You can enter the command to try it when testing. If there is anything you don’t understand, you can discuss it in the comment area below. You may also be interested in:
|
<<: Detailed steps to download Tomcat and put it on Linux
>>: Solve the Linux Tensorflow2.0 installation problem
Enable WSL Make sure the system is Windows 10 200...
The display effects on IE, Fir...
If you are using the latest Ubuntu Server version...
When a request is sent to your server to display ...
1. Basic Environment 1. Operating system: CentOS ...
Sometimes, we need to use the hyperlink <a> ...
Preface When introducing defineProperty before, I...
The experimental code is as follows: </head>...
I'm currently working on my own small program...
Introduction to kubectl kubectl is a command line...
1. Take nginx as an example Nginx installed using...
Original Intention The reason for making this too...
Linux change hostname command 1. If you only need...
First, I will give you the VMware 14 activation c...
This article example shares the implementation of...