Home > Database > Mysql Tutorial > How to Convert Year and Week Number to a Specific Date in MySQL?

How to Convert Year and Week Number to a Specific Date in MySQL?

Mary-Kate Olsen
Release: 2025-01-04 16:56:40
Original
335 people have browsed it

How to Convert Year and Week Number to a Specific Date in MySQL?

Converting Week Number to Date

Question:

How can I convert a given year and calendar week into a specific date, such as Tuesday of that week?

Answer:

Utilizing MySQL's STR_TO_DATE() Function

MySQL's STR_TO_DATE() function provides a convenient one-line solution to this problem. The function converts a given string in a specific format into a DATE data type.

Example:

Suppose we want to determine the date of the Tuesday for the 32nd week of 2013. We can use the following query:

SELECT STR_TO_DATE('2013 32 Tuesday', '%X %V %W');
Copy after login

This query converts the string '2013 32 Tuesday' into a date value using the specified format:

  • %X: Year
  • %V: Week number
  • %W: Day of the week (Tuesday)

Result:

The query will output the date '2013-08-13', which is the Tuesday of the 32nd week of 2013.

This solution offers a concise and efficient way to convert from a week number to a specific date, making it a reliable option for various programming tasks.

The above is the detailed content of How to Convert Year and Week Number to a Specific Date in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template