Use of MySQL date functions

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)

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.