This question already has an answer here:
MYSQL - find nearest previous day
(1 answer)
Closed 2 years ago.
Is there a way to display all records from let's say last Thursday until today in mysql?
I found all records from last week, all records in certain interval of days but nothing for a fixed day last week and all this week...
Our records always begin on Thursday that's why i'm asking
Edit:
This week (no matter if it is Monday to Friday) i want to see all records from last Thursday until NOW(). Next week the whole process repeats but must display records from this Thursday until the day (Monday to Friday) next week... and so on.
select * from table
where column = DATE_SUB(CURDATE(),INTERVAL (WEEKDAY(CURDATE()) +4) DAY);
select * from your_table where your_date_field >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -4)
the -4 in the DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -4) coresponds to last thursday
EDIT use this one:
select * from your_table where DATEADD(dd, DATEDIFF(dd, 0, your_date_field), 0) >= DATEADD(dd, DATEDIFF(dd, 0, '6/14/2018'), 0)
This one will work for you:
select * from your_table where DATEADD(dd, DATEDIFF(dd, 0, your_date_field), 0) >= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(day,-7, DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 3))), 0)
this basically gets your_date_field >= thursday of last week. Will continue to be the last thursday as days and weeks pass.
Related
I'm building a MySQL report that could be run at any point in a given month. When run, it will need to pull through all results where the date of the record is in the month in 5 months' time - e.g. if run any time in December, it will need to pull through any records with a date between 1st May and 31st May 2020, if run in January it would pull through the June records and so on.
Have been searching for a few different date functions but can't seem to find the right one to let me look ahead 5 months using a date interval, but that will only show me from the 1st to the last day of that month.
Closest I seem to have is this (which my query doesn't like):
due_date <= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 6, -1) AND due_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 5, 0
Thanks
You can try below query to get that result -
SELECT adddate(current_date, interval 5 month) curr_date
,DATE_SUB(adddate(current_date, interval 5 month),INTERVAL DAYOFMONTH(adddate(current_date, interval 5 month))-1 DAY) AS '1st of month'
,last_day(adddate(now(), interval 5 month)) last_of_month;
Here is the fiddle.
How to fetch current week data till today's date ?
Example - Current week start from 2017-08-01 to 2017-08-07 and today date is 2017-08-03. I want to fetch data from 2017-08-01 to 2017-08-03 using query.
This is query -
SELECT *
FROM user_data
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
I have resolved my problem.
SELECT *
FROM current_week
WHERE YEARWEEK(`dt`, 1) = YEARWEEK(CURDATE(), 1) and dt <= curdate() order by dt
This is working fine.
This one gets tricky in the last and first weeks of calendar years.
First you need to decide whether your week starts on Sunday or Monday. That is based on your national jurisdiction's business practices. In North America, it's generally Sunday. In Europe it is sometimes Monday.
You need an expression that computes the first moment of a calendar week based on a date.
This is such an expression for weeks starting Sunday.
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
This is it for weeks starting Monday. (Notice the -2.)
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7))
Now you can select stuff from your table that's in the current week.
SELECT whatever, whatever
FROM user_data
WHERE `date` >= FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
If you want stuff from the week before the current week use this
SELECT whatever, whatever
FROM user_data
WHERE `date` >= FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) - INTERVAL 7 DAY
AND `date` < FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
I'm trying to solve a task: I have a table containing information about ships' battles. Battle is made of name and date. The problem is to get the last friday of the month when the battle occurred.
WITH num(n) AS(
SELECT 0
UNION ALL
SELECT n+1 FROM num
WHERE n < 31),
dat AS (
SELECT DATEADD(dd, n, CAST(battles.date AS DATE)) AS day,
dateadd(dd, 0, cast(battles.date as date)) as fight,
name FROM num,battles)
SELECT name, fight, max(day) FROM dat WHERE DATENAME(dw, day) = 'friday'
I thought there must be a maximum of date or something, but my code is wrong.
The result should look like this:
Please, help!!
P.S. DATE_FORMAT is not available
Possible problem: as spencer7593 noticed - and as I should have done and didn't - your original query is not MySQL at all. If you're porting a query that's OK. Otherwise this answer will not be helpful, as it makes use of MySQL functions.
The day you want is number 4 (0 being Sunday in MySQL).
So you want the last day of the month if the last day of the month is a 4; if the day of the month is a 5 you want a date which is 1 day earlier; if the day of the month is a 3 you want a date which is 1 day later, but that's impossible (the month ends), so you really need a date six days earlier.
This means that if the daynumber difference is negative, you want it modulo seven.
You can then build this expression (#DATE is your date; I use a fake date for testing)
SET #DATE='2015-02-18';
DATE_SUB(LAST_DAY(#DATE), INTERVAL ((WEEKDAY(LAST_DAY(#DATE))+7-4))%7 DAY);
It takes the last day of the month (LASTDAY(#DATE)), then it computes its weekday, getting a number from 0 to 6. Adds seven to ensure positivity after subtracting; then subtract the desired daynumber, in this case 4 for Friday.
The result, modulo seven, is the difference (always positive) from the last day's daynumber to the wanted daynumber. Since DATE_SUB(date, 0) returns the argument date, we needn't use IF.
SET #DATE='1962-10-20';
SELECT DATE_SUB(LAST_DAY(#DATE), INTERVAL ((WEEKDAY(LAST_DAY(#DATE))+7-4))%7 DAY) AS friday;
+------------+
| friday |
+------------+
| 1962-10-26 |
+------------+
Your query then would become something like:
SELECT `name`, `date`,
DATE_SUB(LAST_DAY(`date`),
INTERVAL ((WEEKDAY(LAST_DAY(`date`))+7-4))%7 DAY) AS friday
FROM battles;
My problem is, I have a form named 'weekly-off setting' in which I am selecting a one or two days as a weekly off.If I have weekly off on saturday then I want to find out the first saturdays date which is coming in the first week of the selected month.So can anyone tell me the mysql query for this problem.
Thanks in advance.
It's a little unclear from your statement, but you're trying to find the next Saturday?
select date_add(now(), interval 7-dayofweek(now()) day);
Which unfortunately will return today if you're on a Saturday, so the sequence becomes:
SET #OFFSET = 7-dayofweek(now());
SET #OFFSET = IF(#OFFSET = 0, 7, #OFFSET);
select date_add(now(), interval #OFFSET day);
which can be combined into one:
select date_add(now(), interval IF(7-dayofweek(now()) = 0, 7, 7-dayofweek(now())) day) as next;
Suppose I have 2011-01-03 and I want to get the first of the week, which is sunday, which is 2011-01-02, how do I go about doing that?
The reason is I have this query:
select
YEAR(date_entered) as year,
date(date_entered) as week, <-------This is what I want to change to select the first day of the week.
SUM(1) as total_ncrs,
SUM(case when orgin = picked_up_at then 1 else 0 end) as ncrs_caught_at_station
from sugarcrm2.ncr_ncr
where
sugarcrm2.ncr_ncr.date_entered > date('2011-01-01')
and orgin in(
'Silkscreen',
'Brake',
'Assembly',
'Welding',
'Machining',
'2000W Laser',
'Paint Booth 1',
'Paint Prep',
'Packaging',
'PEM',
'Deburr',
'Laser ',
'Paint Booth 2',
'Toolpath'
)
and date_entered is not null
and orgin is not null
AND(grading = 'Minor' or grading = 'Major')
and week(date_entered) > week(current_timestamp) -20
group by year, week(date_entered)
order by year asc, week asc
And yes, I realize that origin is spelled wrong but it was here before I was so I can't correct it as too many internal apps reference it.
So, I am grouping by weeks but I want this to populate my chart, so I can't have all the beginning of weeks looking like different dates. How do I fix this?
If the week starts on Sunday do this:
DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)
If the week starts on Monday do this:
DATE_ADD(mydate, INTERVAL(-WEEKDAY(mydate)) DAY);
more info
If you need to handle weeks which start on Mondays, you could also do it that way. First define a custom FIRST_DAY_OF_WEEK function:
DELIMITER ;;
CREATE FUNCTION FIRST_DAY_OF_WEEK(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN SUBDATE(day, WEEKDAY(day));
END;;
DELIMITER ;
And then you could do:
SELECT FIRST_DAY_OF_WEEK('2011-01-03');
For your information, MySQL provides two different functions to retrieve the first day of a week. There is DAYOFWEEK:
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.
And WEEKDAY:
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
If week starts on Monday
SELECT SUBDATE(mydate, weekday(mydate));
If week starts on Sunday
SELECT SUBDATE(mydate, dayofweek(mydate) - 1);
Example:
SELECT SUBDATE('2018-04-11', weekday('2018-04-11'));
2018-04-09
SELECT SUBDATE('2018-04-11', dayofweek('2018-04-11') - 1);
2018-04-08
Week starts day from sunday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL (1 - DAYOFWEEK("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (7 - DAYOFWEEK("2019-03-31")) DAY) as end_date
Week starts day from Monday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL ( - WEEKDAY("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (6 - WEEKDAY("2019-03-31")) DAY) as end_date
select '2011-01-03' - INTERVAL (WEEKDAY('2011-01-03')+1) DAY;
returns the date of the first day of week. You may look into it.
This is a much simpler approach than writing a function to determine the first day of a week.
Some variants would be such as
SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY) (for the ending date of a query, such as between "beginning date" and "ending date").
SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY (for the beginning date of a query).
This will return all values for the current week. An example query would be as follows:
SELECT b.foo FROM bar b
WHERE b.datefield BETWEEN
(SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY)
AND (SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY))
This works form me
Just make sure both dates in the below query are the same...
SELECT ('2017-10-07' - INTERVAL WEEKDAY('2017-10-07') Day) As `mondaythisweek`
This query returns: 2017-10-02 which is a monday,
But if your first day is sunday, then just subtract a day from the result of this and wallah!
If the week starts on Monday do this:
DATE_SUB(mydate, INTERVAL WEEKDAY(mydate) DAY)
SELECT MIN(DATE*given_date*) FROM *table_name*
This will return when the week started at for any given date.
Keep the good work going!