MySQL Date conversion

Source: Internet
Author: User

One, string to date

Here's how to convert a string into a date in MySQL:

Background: RQ field information is: 20100901

1, do not need to convert:

SELECT * from Tairlist_day WHERE rq> ' 2010-07-31 ' and rq< ' 2010-09-01 '

2. Use: Date_format

SELECT * from Tairlist_day WHERE date_format (RQ, '%y-%m-%d ') > ' 2010-08-01 ' and Date_format (RQ, '%y-%m-%d ') < ' 2010-0 8-03 '

The 1th one to be in a "loose" syntax is allowed: Use any punctuation as a delimiter in the date part and time part, such as:

A number in YYYYMMDD or YYMMDD format, as long as the number looks like a date. For example, 19830905 and 830905 are interpreted as ' 1983-09-05 '.

It is recommended to use the 2nd type

Second, date to string

Mysql> Select Date_format (now (), '%Y '); +-------------------------+| Date_format (now (), '%Y ') |+-------------------------+|                    |+-------------------------+1 row in Set (0.00 sec) Extension:%Y: Year%c: Month%d: Day%h: Hour%i: minutes%s: Seconds mysql> Select Date_format (Now (), '%y-%c-%d%h:%i:%s '); +----------------------------------------+| Date_format (now (), '%y-%c-%d%h:%i:%s ') |+----------------------------------------+| 2009-8-07 06:59:40                     |+----------------------------------------+1 row in Set (0.00 sec)

MySQL Date conversion

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.