MySQL Date and Time Extract function Introduction

Source: Internet
Author: User

The MySQL Date and Time Extract function has the advantage of selecting various parts of the date and time, from year to microsecond, making it easier to process MySQL date and time.

MySQL Date and Time Extract selection) function.
1. Select each part of the date and time: date, time, year, quarter, month, day, hour, minute, second, microsecond

 
 
  1. set @dt = '2008-09-10 07:15:30.123456';  
  2. select date(@dt); -- 2008-09-10  
  3. select time(@dt); -- 07:15:30.123456  
  4. select year(@dt); -- 2008  
  5. select quarter(@dt); -- 3  
  6. select month(@dt); -- 9  
  7. select week(@dt); -- 36  
  8. select day(@dt); -- 10  
  9. select hour(@dt); -- 7  
  10. select minute(@dt); -- 15  
  11. select second(@dt); -- 30  
  12. select microsecond(@dt); -- 123456 

2. MySQL Extract () function, which can implement similar functions:

 
 
  1. set @dt = '2008-09-10 07:15:30.123456';  
  2. select extract(year from @dt); -- 2008  
  3. select extract(quarter from @dt); -- 3  
  4. select extract(month from @dt); -- 9  
  5. select extract(week from @dt); -- 36  
  6. select extract(day from @dt); -- 10  
  7. select extract(hour from @dt); -- 7  
  8. select extract(minute from @dt); -- 15  
  9. select extract(second from @dt); -- 30  
  10. select extract(microsecond from @dt); -- 123456  
  11.  
  12. select extract(year_month from @dt); -- 200809  
  13. select extract(day_hour from @dt); -- 1007  
  14. select extract(day_minute from @dt); -- 100715  
  15. select extract(day_second from @dt); -- 10071530  
  16. select extract(day_microsecond from @dt); -- 10071530123456  
  17. select extract(hour_minute from @dt); -- 715  
  18. select extract(hour_second from @dt); -- 71530  
  19. select extract(hour_microsecond from @dt); -- 71530123456  
  20. select extract(minute_second from @dt); -- 1530  
  21. select extract(minute_microsecond from @dt); -- 1530123456  
  22. select extract(second_microsecond from @dt); -- 30123456  

In addition to date () and time () functions, the MySQL Extract () function must be fully functional. You can also select 'day _ microsecond. Note that we will not only select day and microsecond, but choose from day of the date to microsecond.

MySQL date functions

Provides you with an in-depth understanding of the MYSQL Cast function.

MySQL string segmentation and concatenation statements

View MySQL Default Character Set

Role of MySQL Foreign keys in Databases

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.