WEEKOFYEAR(NOW()) vs WEEK('2018-05-1') - the current date - mysql

just wondering if anyone can shed some light on why why the following query (Note: today's date is 1 May 2018)
SELECT WEEKOFYEAR(NOW()); gives a result of 18.
BUT the query:
SELECT WEEK('2018-05-1') (using all the different modes 0 -7) gives a result of 17?
Shouldn't the week of year for NOW() technically be the same as the week of the year for: 2018-05-01 since today is the 5th of May 2018?
Very curious as to why its not giving the same result.
Many thanks

In weekoftheyear() function, Week number will start from the 1st day of the year.
In Week() function, Week number will start from 1st Monday of the given year.

Related

Previous month query - Databricks

I try to find a function where I can extract the result of the last month only
(for exemple if I launch the query in november, I want to display only the resultat of october)
There the result :
I dont know if I have to enter the function in my select or where clause
Thanks for you help!!
CHeers!
I tried the function month(date, -1)
I want to see all the result for the previous month
You can try getting the date and applying in the where clause.
Note that there may be more efficient options available.
Query to Use:
WHERE
DATE_TRANSACTION BETWEEN
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM')
AND
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE))
Explanation:
CURRENT_DATE - Gives the current Date
dayofmonth(CURRENT_DATE) - Gives the day part of current date.
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)) - Gives the last day of previous month. Assume current_date is 2022-11-23, dayofmonth will give 23, when you subtract 23 days, it goes to the last day of previous month i.e. 2022-10-31.
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM') - Truncates date to first day of month.
DATE_TRANSACTION - between these two days.

MySQL How to get First Sunday and last Saturday of previous month using curdate()

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!!!

MySQL Select From Prior Week, not just 7 days

I am attempting to get a query that selects the week prior (Sun-Sat). I've fought with this query and the closest I can get is the last 7 days, using the following:
SELECT *
FROM dates
WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE();
I'm really unsure how to proceed from here. It seems as if I need to create some kind of relation between CURDATE() and the Saturday before maybe?
Any help is appreciated.
You are after the Week of the year.
Look at the Week Function: WEEK(date[,mode])
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_week
The mode describes how you define the week (which is the start of the week)
WEEK(date,3) is for a week that starts Monday.
SELECT *
FROM dates
WHERE
-- Last Week
WEEK(date,3) = WEEK(CURDATE(),3)-1
AND YEAR(date)= YEAR(CURDATE()) ;
Don't forget the year. Week is just a number between 1 and 52. So the Year is important!
The code above is not correct. it will fail on the last week of the year!

how to get week of year with friday as starting date using mysql

I'm struggling to get week of year (from 1 to 52) in mysql query using friday as starting date. My friend said just add current date with number (because friday is 5 and sunday as default starting date is 0) 5 so that we can know week of year of given date.
What function will satisfy this problem? as for given that if value is 2009-12-28, it will ruin the function itself?
thanks before
i got a trick
select week(from_days(to_days('2010-01-01') + 2)) as week, '2010-01-01'
but if someone can give better answer, i will appreciate since i'm not sure about my method
also if i give '2010-12-31' as answer, it's stuck

How To Get YEARWEEK() To See Sunday As The Start Of The Week?

I'm trying to get the YEARWEEK function to see Sunday as the 1st day of the week.
An example date is: Sunday 1st Mar 2009
Here is my sql
SELECT YEARWEEK('2009-03-01')
and the result is
-> 200909
Which is week 9. I believe it is telling me this is week 9 because it sees the sunday as the last day of the previous week.
How would I get this function to see sunday as the 1st day of the week and therefore return as week 10?
According to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_yearweek, YEARWEEK has an optional second argument which controls this. YEARWEEK('2009-03-01', 0) should do what you want (but see the table of possible values under WEEK on that page).
this will do it:
SELECT YEARWEEK('2009-03-01')+IF(WEEKDAY('2009-03-01')=6,1,0);
EDIT: this is the better solution, for more information click here:
SELECT YEARWEEK('2009-03-01',0);
(but i don't know why you want to do this - 2009.03.01 is in week 9, not 10...)