Problems with date columns in MySQL

Source: Internet
Author: User
Tags comparison datetime idate numeric value

The date value is in the format ' YYYY-MM-DD '. According to standard SQL, no other format is allowed. You should use this format in the update expression and in the WHERE clause of the SELECT statement. For example:

Mysql> SELECT * from tbl_name WHERE date >= ' 2003-05-05 ';

For convenience, if the date is used in a numerical environment, MySQL automatically converts the date to a numeric value (and vice versa). It also has considerable intelligence to allow a "loose" string form ("loose form") to be used as a separator between parts, either at update time or in a WHERE clause that compares dates with timestamp, date, or datetime columns. For example, ' 2004-08-15 ' and ' 2004#08#15 ' are equivalent. MySQL can also convert strings that do not contain any delimiters (such as ' 20040815 '), and the precursor is the date it must be meaningful.

When comparing date, time, DateTime, or timestamp with a constant string using the <, <=, =, >=, >, or between operator, MySQL typically converts a string into an inner-ministerial integer for fast comparison ( And a slightly "loose" string check). However, the conversion has the following exceptions:

When comparing two columns

When comparing a date, time, datetime, or timestamp column to an expression

When using other comparison methods, such as in or strcmp ().

For these exceptions, the object is converted to a string and a string comparison is performed, using this method for comparison.

To keep it safe, suppose you compare strings by string, and if you intend to compare temporary values and strings, use the appropriate string function.

For special date ' 0000-00-00 ', can be saved and retrieved in ' 0000-00-00 ' form. When you use the ' 0000-00-00 ' date in Myodbc, the date is automatically converted to null for MYODBC 2.50.12 or later, because ODBC cannot handle such dates.

Since MySQL is able to perform the transformations described earlier, the following statements work correctly:

Mysql> INSERT into Tbl_name (Idate) VALUES (19970505);

Mysql> INSERT into Tbl_name (idate) VALUES (' 19970505 ');

Mysql> INSERT into Tbl_name (idate) VALUES (' 97-05-05 ');

Mysql> INSERT into Tbl_name (idate) VALUES (' 1997.05.05 ');

Mysql> INSERT into Tbl_name (idate) VALUES (' 1997 05 05 ');

Mysql> INSERT into Tbl_name (idate) VALUES (' 0000-00-00 ');

Mysql> SELECT idate from tbl_name WHERE idate >= ' 1997-05-05 ';

Mysql> SELECT idate from Tbl_name WHERE idate >= 19970505;

Mysql> SELECT MOD (idate,100) from Tbl_name WHERE idate >= 19970505;

Mysql> SELECT idate from tbl_name WHERE idate >= ' 19970505 ';

Note : Please pay attention to the triple tutorial channel for more wonderful tutorials,

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.