MySQL Date time function encyclopedia

Source: Internet
Author: User
Tags month name mysql range

For a description of the range of values owned by each type and the valid format for the specified date and time value, see 7.3.6 Date and Time type.

Here is an example of using a date function. The following query selects all records and its date_col value is within the last 30 days:

mysql> SELECT something FROM table 
WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;

DayOfWeek (date)

Returns the week index of date (1= Sunday, 2 = Monday, ...). 7= Saturday). These index values correspond to ODBC standards.

mysql> select DAYOFWEEK(@#1998-02-03@#); 
-> 3

Weekday (date)

Returns the week index of date (0= Monday, 1 = Tuesday, ...). 6= Sunday).

mysql> select WEEKDAY(@#1997-10-04 22:23:00@#); 
-> 5 
mysql> select WEEKDAY(@#1997-11-05@#); 
-> 2

DayOfMonth (date)

Returns the date in the month of date, in the range 1 through 31.

mysql> select DAYOFMONTH(@#1998-02-03@#); 
-> 3

DayOfYear (date)

Returns the number of days in a year, in the range of 1 to 366.

mysql> select DAYOFYEAR(@#1998-02-03@#); 
-> 34

MONTH (date)

Returns the month of date, ranging from 1 to 12.

mysql> select MONTH(@#1998-02-03@#); 
-> 2

Dayname (date)

Returns the name of the week of date.

mysql> select DAYNAME("1998-02-05"); 
-> @#Thursday@#

MonthName (date)

Returns the month name of the date.

mysql> select MONTHNAME("1998-02-05"); 
-> @#February@#

Quarter (date)

Returns the quarter of date one year, ranging from 1 to 4.

mysql> select QUARTER(@#98-04-01@#); 
-> 2

WEEK (date)

WEEK (Date,first)

For Sunday is the first day of the week, there is a single parameter that returns the week number of date, ranging from 0 to 52. 2 Parameter Form week () allow

Do you specify whether the week starts in Sunday or Monday. If the second argument is 0, the week begins in Sunday, and if the second argument is 1,

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.