Usage of MySQLCAST and CONVERT Functions

Source: Internet
Author: User
The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The syntax is as follows: CAST (valueastype); CONVERT (value, type); CAST (xxxAS type), CONVERT (xxx, type ). There are limits on the types that can be converted. This type can be one of the following values:

The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The syntax is as follows: CAST (value AS type); CONVERT (value, type); CAST (xxx as type), CONVERT (xxx, type ). There are limits on the types that can be converted. This type can be one of the following values:

The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The specific syntax of the two is as follows:

CAST(value as type);CONVERT(value, type);

Is CAST (xxx AS type), CONVERT (xxx, type ).

There are limits on the types that can be converted. This type can be one of the following values:

  • Binary, which has the same effect as BINARY? ?
  • Character type, which can contain parameters: CHAR ()? ??
  • DATE: DATE? ??
  • TIME: TIME? ??
  • DATETIME type: DATETIME? ??
  • Floating Point: DECIMAL? ? ?
  • Integer: SIGNED? ??
  • UNSIGNED integer: UNSIGNED?

The following are examples:

Example 1

mysql> SELECT CONVERT('23',SIGNED);+----------------------+| CONVERT('23',SIGNED) |+----------------------+|                   23 |+----------------------+1 row in set

Example 2

mysql> SELECT CAST('125e342.83' AS signed);+------------------------------+| CAST('125e342.83' AS signed) |+------------------------------+|                          125 |+------------------------------+1 row in set

Example 3

mysql> SELECT CAST('3.35' AS signed);+------------------------+| CAST('3.35' AS signed) |+------------------------+|                      3 |+------------------------+1 row in set

As in the preceding example, convert varchar to int using cast (a as signed), where a is a string of the varchar type.

Example 4

In SQL Server, the following code demonstrates that the hexadecimal storage of the Date Storage represents only the date and time when the datetime variable contains the pure date.

DECLARE @ dt datetime -- simple date SET @ dt = '2017-1-2 'select cast (@ dt as binary (8) -- result: 0x0000000100000000 -- simple time SET @ dt = '00: 00: 01 'select cast (@ dt as binary (8) -- result: 0x0000000000000000012c

MySQL type conversion is similar to SQL Server, that is, the type parameters are a little different: CAST (xxx AS type), CONVERT (xxx, type ).

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.