The use of the MySQL cast and CONVERT functions

Source: Internet
Author: User

MySQL's cast () and CONVERT () functions can be used to get a value of one type and produce a value of another type. The specific syntax for both is as follows:

1 CAST(value astype);
2 CONVERT(value, type);

is cast (xxx as type), CONVERT (XXX, type).

There is a limit to the types that can be converted. This type can be one of the following values:

    • Binary, with binary prefix effect: binary
    • Character type, with parameters: CHAR () What are the casinos in Macau?
    • Date: Date
    • Time:
    • DateTime Date/Time type
    • Floating point number: DECIMAL
    • Integer: Signed
    • unsigned integer: UNSIGNED

Here are a few examples:

Example One

1 mysql> SELECTCONVERT(‘23‘,SIGNED);
2 +----------------------+
3 CONVERT(‘23‘,SIGNED) |
4 +----------------------+
5 |                   23 |
6 +----------------------+
7 1 row inset

Example Two

1 mysql> SELECTCAST(‘125e342.83‘ ASsigned);
2 +------------------------------+
3 CAST(‘125e342.83‘ASsigned) |
4 +------------------------------+
5 |                          125 |
6 +------------------------------+
7 1 row inset

Example Three

1 mysql> SELECTCAST(‘3.35‘ ASsigned);
2 +------------------------+
3 CAST(‘3.35‘ASsigned) |
4 +------------------------+
5 |                      3 |
6 +------------------------+
7 1 row inset

As in the example above, the varchar is converted to int with cast (a as signed), where a is a varchar-type string.

Example 4

In SQL Server, the following code demonstrates the result of a DateTime variable that contains only a single date and a simple time when the date stores the hexadecimal store representation.

01 DECLARE @dt datetime
02  
03 --单纯的日期
04 SET @dt=‘1900-1-2‘
05 SELECT CAST(@dt asbinary(8))
06 --结果: 0x0000000100000000
07  
08 --单纯的时间
09 SET @dt=‘00:00:01‘
10 SELECT CAST(@dt asbinary(8))
11 --结果: 0x000000000000012C

MySQL type conversion is like SQL Server, that is, the type parameter is a little different: CAST (xxx as type), convert (XXX, type).

The use of the MySQL cast and CONVERT functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.