I easily get Week Number using MySql Week function like this WEEK(SYSDATE())
I just wanted to know how to get from and to date using week number in MySQL.
Result required:
Week No From date To date
Week 25 June 18, 2012 June 24, 2012
This works great for this kind of job:
SELECT
ADDDATE(CURDATE(), INTERVAL 1-DAYOFWEEK(CURDATE()) DAY) _From,
ADDDATE(CURDATE(), INTERVAL 7-DAYOFWEEK(CURDATE()) DAY) _To;
Beware that first day of week here is Sunday. It may depends on your country convention ;)
Related
I have a column of time with type datetime in mysql called createdAt, so I want find out the data with time range like this
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate()))
and createdAt <= ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()))
assume createdAt curdate() based on this question made are october 8th 2020, so that time range will be 28th september 2020 until 04th october 2020
But when I check manually, if I add some time range in another media, why the counting is little bit more different? For example: using this query, count of transaction is 87 transaction, but then I used another media to count the transaction between 28th september 2020 00:00:00 until 04th october 23:59:59 its 90 transaction, is that any I can add in query for time range in my query date range?
Your expression ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate())) returns only date part of last week day, so all data after start this day is not included.
You have couple of options to solve this:
Round createdAt to date withot time part:
SELECT *
FROM your_table
WHERE DATE(createdAt) BETWEEN
SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()));
Use strong less condition with next day:
SELECT *
FROM your_table
WHERE
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
createdAt < ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 7-WEEKDAY(curdate()));
Essentially what I want to obtain is the monday given the number of the week::
I have week: 9, 7,5
This number of week corresponds to a timestamp:
2019-03-02 02:48:00,
2019-02-15 02:58:00,
2019-01-31 00:25:00
I want to obtain the date of the monday of this week(first day of week):
2/25,
2/11,
1/28
How can extract this outout? it can be extracted from timestamp if it is easier
If you want to get Monday of the week.
You can try to use DATE_ADD and WEEKDAY function.
SELECT DATE_ADD(dt, INTERVAL - WEEKDAY(dt) DAY)
FROM T
sqlfiddle
EDIT
There is another function DATE_FORMAT represent to the date format string.
using "%m/%d"
SELECT DATE_FORMAT(
DATE_ADD(dt, INTERVAL - WEEKDAY(dt) DAY), "%m/%d")
FROM T
sqlfiddle
I have MySQL condition that grabs a time interval from now back x number of months. Typically, this will be set to 13 months so you can compare the current month to that of last year.
'created > DATE_SUB(now(), INTERVAL ' . $timeInterval . ' MONTH)'
So for example last January compared to this January, but I'd like to include all of the previous years month. So instead of January 20, 2015 to January 20, 2016 I would have January 01, 2015 to the current date in January this year until February 1st.
I'd use DATE_FORMAT to make it quick and easy, replace the "day" part of the date with a constant. Then subtract your number of months...
... t.created > DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL ? MONTH
As a demonstration of what is returned by that expression, we can test it using a simple SELECT statement:
SELECT NOW(), DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 12 MONTH
NOW() DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 12 MONTH
------------------- -------------------------------------------------
2016-01-27 21:01:02 2015-01-01
FOLLOWUP
Are you sure you want a "greater than" comparison, rather than a "greater than or equal to" comparison >= ?
There are other approaches to generating that date value to compare to. You could use DATE(NOW()) or CURDATE() to return the current date with no time component.
And use the DAY() function to get the numeric value of the current day, and then subtract that (minus 1) as a number of days. For example, something like this:
>= DATE(NOW()) - INTERVAL DAY(NOW())-1 DAY - INTERVAL 12 MONTH
That seems messier and more complicated. I think it's easier to understand stuffing in the '-01' as the day part.
created > str_to_date(concat(year(now())-1, '-01-01'), '%Y-%m-%d')
Or if you need not all previous year:
select str_to_date(concat(year(now())-1, '-', month(now()),'-01'), '%Y-%m-%d')
I need to get the last month's dates from 1st to current date. Suppose if today's date is March 25th, I need to get the dates from 1st to 25th of february. Suppose if today's date is March 30th, I need to get the dates from 1st to 28/29th Feb, whatever the maximum final date is available. I have searched a lot to get that, but no luck. Can someone please help me how to get this special case done? I am able to do it on another database, but I want to do this on mysql.
Basically what I did for other database is this --> date between date(to_char(date(add_months(DATE(sysdate) ,-1)),'YYYY-MM-01 00:00:00')) and date(add_months(DATE(sysdate) ,-1))
This should do what you want:
WHERE d BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m-01')
AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
You can use DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)). This will automatically limit the result to the last day of the month, so if today's date is March 30th, this will return Feb 28th.
For "2012-07-12", how can I get the start of the week, i.e., "2012-07-08", and start of the month, i.e., "2012-07-01"?
First day of the month:
SELECT DATE_FORMAT('2007-07-12', '%Y-%m-01');
output: 2007-07-01
First day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFWEEK('2007-07-12')-1 DAY);
output: 2007-07-08
MySQL reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Same answer as Borophyll's, but I have changed the behavior of the first day of the month to return a date, not just a string which avoids date formatting/parsing mentioned in user151220's answer.
First day of the month:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFMONTH('2007-07-12') - 1 DAY);
output: 2007-07-01
First day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFWEEK('2007-07-12') - 1 DAY);
output: 2007-07-08
MySQL reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
For those who need Monday as the first day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL WEEKDAY('2007-07-12') DAY);
output: 2007-07-09
This relies on the WEEKDAY function, which starts with Monday instead of DAYOFWEEK, which starts with Sunday.
The DATE_FORMAT reply from Borophyll is very good, but gives a string rather than a date. So can't be compared easily.
If you need to use this as a comparison to a date field, use str_to_date to reverse it back to date rather than string.
select x from y where date >= str_to_date( DATE_FORMAT(now()-interval 12 month,'Y-%m-01'), '%Y-%m-%d')
If you are (say) looking at 12 months sales figures, but you want to always start off from the 1st of a month.
This will work if you want to just code it and forget about it, it will use datetime now and always return MTD results-
where date_completed between date_sub(date(now()), INTERVAL dayofmonth(now()) -1 day) and now()