Conversion of different Mysql Date and Time formats

Source: Internet
Author: User
Tags month name

The following article describes how to convert the date and time formats of Mysql, the date and specific time types of 7.3.6 can be seen in the description of the range of values related to different types and the valid format of the specified date and time values. Here is an example of using the mysql date function.

The following query Selects all records. The value of date_col is within the last 30 days:

 
 
  1. mysql> SELECT something FROM table   
  2. WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30;   
  3. DAYOFWEEK(date)   

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

 
 
  1. mysql> select DAYOFWEEK('1998-02-03');   
  2. -> 3   
  3. WEEKDAY(date)   

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

 
 
  1. mysql> select WEEKDAY('1997-10-04 22:23:00');   
  2. -> 5   
  3. mysql> select WEEKDAY('1997-11-05');   
  4. -> 2   
  5. DAYOFMONTH(date)   

Returns the mysql date in the month of date in the range of 1 to 31.

 
 
  1. mysql> select DAYOFMONTH('1998-02-03');   
  2. -> 3   
  3. DAYOFYEAR(date)   

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

 
 
  1. mysql> select DAYOFYEAR('1998-02-03');   
  2. -> 34   
  3. MONTH(date)   

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

 
 
  1. mysql> select MONTH('1998-02-03');   
  2. -> 2   
  3. DAYNAME(date)   


Returns the week name of date.
 

 
 
  1. mysql> select DAYNAME("1998-02-05");   
  2. -> 'Thursday'   
  3. MONTHNAME(date)   

Returns the month name of date.

 
 
  1. mysql> select MONTHNAME("1998-02-05");   
  2. -> 'February'   
  3. QUARTER(date)   

Mysql returns the quarter of the year from date, ranging from 1 to 4.

 
 
  1. mysql> select QUARTER('98-04-01');   
  2. -> 2   
  3. WEEK(date)   
  4. WEEK(date,first)   

If Sunday is the first day of a week, there is a single parameter that returns the number of weeks of the date, ranging from 0 to 52. Two Parameter formats: WEEK () allows you to specify whether the WEEK starts on Sunday or Monday. If the second parameter is 0, the week starts from Sunday, and if the second parameter is 1, it starts from Monday.

 
 
  1. mysql> select WEEK('1998-02-20');   
  2. -> 7   
  3. mysql> select WEEK('1998-02-20',0);   
  4. -> 7   
  5. mysql> select WEEK('1998-02-20',1);   
  6. -> 8   
  7. YEAR(date)   

Returns the year of date, ranging from 1000 to 9999.

 
 
  1. mysql> select YEAR('98-02-03');   
  2. -> 1998   
  3. HOUR(time)   

The above content is an introduction to mysql date and time format conversion. I hope you will have some gains.

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.