MYSQL date_format and STR_TO_DATE

Source: Internet
Author: User

The date_format and STR_TO_DATE of MYSQL are recently used in the project. Have a good time with the MYSQL date conversion function. The process is as follows:

Take the current year: SELECT DATE_FORMAT (CURDATE (), '% Y year'); output: SELECT DATE_FORMAT (CURDATE (), '% Y % mmonth'); output: Week number of the current year in month 1, 07th: SELECT DATE_FORMAT (CURDATE (), '% Y % u week'); output: Week 1, 28th

 

OK. Are you sure you want to get all of them? What's better: After you get them, I wonder if I can turn them back? Start now! Convert 2013 to the time object:
SELECT STR_TO_DATE (year (CURDATE (), "% Y") = STR_TO_DATE ('August 1', "% Y years"); output: 1

 

It seems that there is a drama! Continue to change the date of January 1, 07th to the following:
SELECT date_format (CURDATE (), "% Y-% m") = date_format (STR_TO_DATE ('1970, 07th month', "% Y % m month "), "% Y-% m"); output: 1

 

No problem! Continue to set the 28th turnover time in April as follows:
SELECT date_format (CURDATE (), "% Y-% u") = date_format (STR_TO_DATE ('Week 2013, January 1, 28th ', "% Y % u Week "), "% Y-% u"); output: 0
WHY ???? Continue to find the cause and find a magical phenomenon:
SELECT date_format (STR_TO_DATE ('Week 28th, February 11', "% Y % u Week"), "% Y-% u ");

 

The output result is: 2013-613566753 Q & A has no reference at all. After a long headache, I finally thought of the following solution: I still compared it with the 2013 week of June 28th:
SELECT date_format (CURDATE (), '% y') = date_format (STR_TO_DATE ('1970, 28th Week', "% Y"),' % y ') and date_format (CURDATE (), '% U') = (select SUBSTR ('1970, 28th Week' FROM 7 FOR 2); output: 1MYSQL date_format and STR_TO_DATE

 

I recently worked on a project and had a good time with the MYSQL date conversion function. The process is as follows:
Take the current year: SELECT DATE_FORMAT (CURDATE (), '% Y year'); output: SELECT DATE_FORMAT (CURDATE (), '% Y % mmonth'); output: Week number of the current year in month 1, 07th: SELECT DATE_FORMAT (CURDATE (), '% Y % u week'); output: Week 1, 28th

 

Okay. It's nice to get all of them. The better one is as follows: (Hey, everybody can be evil !) After obtaining it, I thought about whether I could turn it back? Start now! Convert 2013 to the time object:
SELECT STR_TO_DATE (year (CURDATE (), "% Y") = STR_TO_DATE ('August 1', "% Y years"); output: 1

 

It seems that there is a drama! Continue to change the date of January 1, 07th to the following:
SELECT date_format (CURDATE (), "% Y-% m") = date_format (STR_TO_DATE ('1970, 07th month', "% Y % m month "), "% Y-% m"); output: 1

 

No problem! Continue to set the 28th turnover time in April as follows:
SELECT date_format (CURDATE (), "% Y-% u") = date_format (STR_TO_DATE ('Week 2013, January 1, 28th ', "% Y % u Week "), "% Y-% u"); output: 0

 

WHY ???? Continue to find the cause and find a magical phenomenon:
SELECT date_format (STR_TO_DATE ('Week 28th, February 11', "% Y % u Week"), "% Y-% u ");

 

The output result is: 2013-613566753 Q & A has no reference at all. After a long headache, I finally thought of the following solution: I still compared it with the 2013 week of June 28th:
SELECT date_format (CURDATE (), '% y') = date_format (STR_TO_DATE ('1970, 28th Week', "% Y"),' % y ') and date_format (CURDATE (), '% U') = (select SUBSTR ('1970, 28th Week' FROM 7 FOR 2); output: 1

 

 

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.