I have the week number and year as separate fields in MySQL, how do I compute the last day of the week(Sunday) in the given year?
SELECT STR_TO_DATE('201038 Sunday', '%X%V %W');
This gives you sunday of the current week (38/2010). I used sunday because the "last day" is not consistend. sometimes its sunday, sometimes saturday (week starts on monday)...
Related
I am having problems with date formats, was trying many formatting solutions but non of them was working.
I do have table with dates and I am summing repeating dates:
SELECT
tt.time,
DATE_FORMAT(tt.time, '%x-%v') AS time_label,
SUM(value) AS value
FROM
time_table tt
GROUP BY DATE_FORMAT(tt.time, '%x-%v')
ORDER BY time ASC
As we can see it's formatting end year date as a new year date. w3school %x is saying
Year for the week where Monday is the first day of the week. Used with %V
and for %v
Week where Monday is the first day of the week (01 to 53). Used with %X
The first week of the year 2020 started on Dec 30, 2019. See Week Numbers for 2020.
Therefore 2019-12-30 12:42:53 is being formatted correctly as 2020-01.
Select week('2017-08-03')
it gives me 31 as week number but it should be 32.
By googling I tried
select week('2017-08-03',4)
still I only get 31.
Here based on the input date given i'm trying to get the week number considering week starts on Thursday and end on Wednesday.
Any help in this is much appreciated
Thanks
From the documentation, the mode argument to WEEK specifies whether the week starts on Sunday or Monday, and how weeks are counted. There's no option to start on Thursday. Here are the meanings of each value:
So 4 means that the week starts on Sunday, week numbers start at 0, and week 1 is the first week with 4 or more days in the year.
See MySQL query yearweek of the current week to start on a Thursday, end on Wednesday for how to do what you want.
I really need help with the correct way to get the first sunday of the previous month and the last Saturday of the previous date using curdate().
This is what I am using for the current month:
select date_add(curdate(),interval -DAY(curdate())+2 DAY)
select adddate(last_day(curdate()),-mod(weekday(adddate(last_day(curdate()),6)),2))
But I am looking for guidance on the correct SQL for the previous month. My goal is the get the last rolling 12 months data broken out by month over month but cannot get the between the first sunday and last saturday correct. Please help!!!
I'm looking for the MySQL query to select date records that fall within the next week using Sunday - Saturday as the format for the week.
So in other words, I'm not looking to get the dates a week from today, I'm looking to get the dates that fall within Sunday - Saturday of the next week.
I found this: MySQL Query to select data from last week? and it works for the previous week from Sunday - Saturday but I'm not sure how to tweak this to get the dates for the next week.
Any ideas?
SELECT * FROM table
WHERE
date > date_add(curdate(),INTERVAL(7-dayofweek(curdate()))DAY)
AND date <= date_add(curdate(),INTERVAL(14- dayofweek(curdate()))DAY)
You can use:
SELECT *
FROM YourTable
WHERE WEEK(YourDateField, 6) = WEEK(CURRENT_DATE + INTERVAL 7 DAY, 6)
for selecting recods of next week (starting with sunday)
Weeks count is 1 to 53 with first week of the year having at least 4 days in the year
We have customers that currently have defined weeks starting either on Sat, Sun or Monday. Came across these DATE_FORMAT options which nicely handle the week starting on Sunday and Monday but can't find a way to do same for week starting on Saturday. Any suggestions?
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
I had a similar issue: I needed to calculate week numbers based on the following rules:
Week starts on Friday
The remainder days of an year (all the days after the last Friday of the year that do not complete a week) should be counted in the
first week of the next year.
For example:
27/12/2012 (Thursday) should be Week 52 of 2012
28/12/2012 (Friday) should be Week 1 of 2013
Week 1 2013 goes from 28/12/2012 to 3/1/2013
I made this statement that calculates both the YEAR and WEEKNUMBER based on these rules that you can easily adapt to your circunstance:
SELECT IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, YEAR(current_date)+1, YEAR(current_date)),
IF(ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7) > 52, 1, ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7));
The tricky part is just this expression:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+1 )/7)
The rest (If clauses) are just for adapting the result of the expression to make year+1 and week = 1 on week 53.
I'll try to explain the expression as best as I can. The following expression gives you the week number pure simple (the day of the year divided by 7 days in a week rounded up):
ceil(( dayofyear(current_date))/7)
But now you want to make it start on Friday (or any other day). To do this you need to add to the current day, the days of the first week that were part of the previous year (it's like your current actually started a few days before, because your first week contains days from the previous year).
This expression calculates that offset based on the weekday on Jan/1:
dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET
The offset is the difference between 7 and the weekdaynumber you want the week to start:
0 for Saturday
1 for Friday
2 for Thursday
3 for Wednesday
...
So now you just have to add it to the previous one resulting in the above mentioned expression that calculates the week numbers starting on any weekday and assuming week 1 to start on the previous year:
ceil(( dayofyear(current_date) + dayofweek(date_format(current_date, '%Y-01-01'))+OFFSET )/7)
Then I just added an IF that turns week 53 into week 1 and another to add 1 to the year if it's week 53.
It took me a while to think on this question.
ISO standard defines the first week to start on Monday and contain 4th day of the year.
MySQL's functions provide much more choices.
date_format() flags %U and %u are using notation where first week is the one where Sunday or Monday is first met. As this is not according to the ISO, I will provide both variants.
If you want to count week numbers starting from Saturday and first year's week is the one containing Saturday, you can use one of the following expressions:
SELECT sign(dayofweek(current_date) - 7) + ceil(dayofyear(current_date)/7);
SELECT ceil((dayofyear(current_date)+
(dayofweek(date_format(current_date, '%Y-01-01'))%7-7))/7);
If first year's week is the one where 4th day of the year falls into, use:
SELECT ceil((dayofyear(current_date)+
(dayofweek(date_format(current_date, '%Y-01-04'))%7-4+1))/7);
The very first expression is quite straightforward.
I will elaborate on the 2nd and 3rd ones. I calculate week number by taking current day of the year, dividing by 7 and ceiling up, quite simple. Week number needs to be adjusted based on the situation at the beginning of the year though.
for the first case (first week starts with the first Saturday), I take day-of-week for the Jan/1 of the year in subject, make Saturday as the day 0 and then adjust day-of-year by the difference. This this makes all days before first saturday yielding negative adjustment number and it ceils up to zero;
for the second case (first week is the one where 4 day of the year falls in), I take day-of-week for the Jan/4 of the year in subject, make Saturday as the day 0. The -4+1 formula gives adjustment to the first Saturday before Jan/4, +1 is used as days of the year starts from 1, not from 0. Negative adjustment means 1st day of the year is not in the first week of the year.
Here're some test dates on the SQL Fiddle.
If you want to count weeks from any other day, you just have to change the formula, making that day being 0 in the sequence. Say, to count weeks starting from Wednesday, use:
SELECT ceil((dayofyear(current_date)+
((dayofweek(date_format(current_date, '%Y-01-04'))+3)%7-4+1))/7);
+3 is used as it complements dayofweek() value for Wednesday to the 7.
Make an adjustment based on DAYOFWEEK().