MySQL conversion type

Source: Internet
Author: User

Today, there is a problem, that is, when a project design table, the time in 20160101123596 in this form, the varchar into the database.

Today to write a SQL, query a report, the condition is to filter this time, but varchar type, how to do?

After Google gets this answer, now summarize

CAST (field as type);

field is a database field, can be any type, and type is the type you want to convert, type has the following

    • 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

I was excited to see this, and I wrote a SQL test:

Select cast ('20151025162337'asDATETIME);

Successful, converted directly into a date format.

Select CAST ('123' as signed) + 2

Successful, shown as 125.

Since this is OK, then for a data table, then there is no problem, such as the next SQL is also written out

Select *,CASTasDATETIME as from table name where hx_time > ' 2015-05-01 '

MySQL conversion 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.