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:

CAST (value as type); 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 ()
    • Date: Date
    • Time:
    • DateTime Date/Time type
    • Floating point number: DECIMAL
    • Integer: Signed
    • unsigned integer: UNSIGNED

Here are a few examples:

Example One

mysql> SELECT CONVERT (' signed '); +----------------------+| CONVERT (' signed ', |+----------------------+|                   |+----------------------+1 Row in Set

Example Two

mysql> SELECT CAST (' 125e342.83 ' as signed) +------------------------------+| CAST (' 125e342.83 ' as signed) |+------------------------------+|                          |+------------------------------+1 Row in Set

Example Three

mysql> SELECT CAST (' 3.35 ' as signed) +------------------------+| CAST (' 3.35 ' as signed) |+------------------------+|                      3 |+------------------------+1 row in Set

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.

DECLARE @dt datetime--Simple date set @dt = ' 1900-1-2 ' SELECT CAST (@dt as binary (8))--Result: 0x0000000100000000--Simple time set @dt = ' 00:00:01 ' SELECT CAST (@dt as binary (8))-Results: 0x000000000000012c

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

This article turns from http://www.nowamagic.net/librarys/veda/detail/2044

The use of the MySQL cast and CONVERT functions

Related Article

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.