The function of getting time in MySQL

Source: Internet
Author: User

To do the project today, there is a requirement to get data for the date seven days ago. Then on Baidu MySQL in the function of time. Collected a few. It is now recorded as follows:

Reference Address: http://www.w3school.com.cn/sql/sql_dates.asp


--Get the current time
SELECT Now ()
SELECT Curdate ()
SELECT current_date ()
SELECT Current_time ()
SELECT Current_timestamp ()
SELECT Curtime ()

--date_sub () function subtracts a specified time interval from a date

Syntax: date_sub (date,interval expr type)

Type value
Microsecond
SECOND
MINUTE
HOUR
Day
WEEK
MONTH
QUARTER
Year
Second_microsecond
Minute_microsecond
Minute_second
Hour_microsecond
Hour_second
Hour_minute
Day_microsecond
Day_second
Day_minute
Day_hour
Year_month

Case: Get seven days ago

Select Date_sub (Curdate (), Interval 6 day)

--Convert to String

Case: Select Date_format (Date_sub (Curdate (), Interval 6 day), '%y-%c-%d%h:%i:%s ')

Syntax: Date_format (Date,format)

format Description
%a Abbreviated Week name
%b Abbreviated month name
%c Month, value
%d Day of the month with English prefixes
%d Day of the month, value (00-31)
%e Day of the month, value (0-31)
%f Microseconds
%H Hours (00-23)
%h Hours (01-12)
%I Hours (01-12)
%i Minutes, value (00-59)
%j Days of the Year (001-366)
%k Hours (0-23)
%l Hours (1-12)
%M Month Name
%m month, value (00-12)
%p AM or PM
%r Time, 12-hour (Hh:mm:ss AM or PM)
%s Seconds (00-59)
%s Seconds (00-59)
%T Time, 24-hour (HH:MM:SS)
%u Week (00-53) Sunday is the first day of the week
%u Week (00-53) Monday is the first day of the week
%V Week (01-53) Sunday is the first day of the week, with%x
%v Week (01-53) Monday is the first day of the week, with%x
%W Week name
%w Day of the week (0= Sunday, 6 = Saturday)
%x year, of which Sunday was the first day of the week, 4-bit, with%V used
%x year, of which Monday was the first day of the week, 4-bit, with%v used
%Y Year, 4 guests
%y Year, 2 guests

The function of getting time in MySQL

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.