With the query below, I generate rows of the week numbers in a year.
SET @sql = 52;
SELECT WEEKOFYEAR("2023-12-31") INTO @sql;
WITH RECURSIVE weeks(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM weeks
WHERE i + 1 <= @sql
)
SELECT * FROM weeks;
The problem is 2023-12-31
,
Imagine in next year 2024-12-31
, so
SELECT WEEKOFYEAR("2023-12-31"). ===> 1
So the result will be buggy, coz the first query just have a row.
2
Answers
You can use the
WEEK()
function to retrieve the week number for a specific date.Update:
You can create a CTE and then call it as a function. I am just implementing the CTE here and select query, hope you can use in a way that you want.
*REMOVED WRONG CODE as it is not striking out in HTML textI hope this is the thing you’re looking for. Based on start_date the week number will be dynamically updated.
Update Output where
start_date='2023-12-31'