get mysql record older than one month only - mysql

How to get mysql record older than 30 days? my code will get all the records even which are inserted two months ago .
WHERE date < DATE_SUB(NOW(), INTERVAL 1 MONTH)
I want only one month ago not bigger than one month

Put both start and end date in the filter.
WHERE date >= CURDATE() - INVERVAL 2 MONTH
AND date < CURDATE() - INTERVAL 1 MONTH
It's verbose and repetitive, but that's an affliction of all SQL code.
Calendar months? If you, on May 7th or anytime in May, want to ask for the calendar month of April, it would be this
WHERE date >= LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH
AND date < LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
LAST_DAY('2021-05-07') gets you '2021-05-31',
+ INTERVAL 1 DAY then gets you '2021-06-01', then
- INTERVAL 2 MONTH finally gets you '2021-04-01'
It's easy to read and reason about.
CURDATE() gives today's date in place of the current date and time given by NOW(). Lots of historical reporting doesn't care about time of day, just calendar day. So it might be smart to use CURDATE(), depending on your application.

Related

Last week (Monday to Sunday) projects SQL

I'm making an SQL query that pulls projects that started in the last week, ideally I want to be able to run this any day of the week and get the same result whether to run it tuesday or friday..
I think this works, only problem is that it starts counting back from saturday instead of sunday..
HAVING MIN(p.start_date) BETWEEN DATE_SUB(CURDATE(),
INTERVAL (DAYOFWEEK(CURDATE()) + 7) DAY) AND DATE_SUB(CURDATE(),
INTERVAL (DAYOFWEEK(CURDATE())) DAY)
You can use yearweek():
where yearweek(min(p.startdate)) = yearweek(curdate() - interval 7 day)

SQL query: A view which gathers from today last month and 2 month in advance

I'm amending a current query which I run on a fairly regular basis for a membership team looking at recent expiries. The clause in that query is:
and date_expiry between '2019-11-01' and '2019-12-31'
The dates are expanded to cover a 2 month period.
What I'd like to do is to create this query as an excel view in which they can refresh as an when they want.
What I have so far and works to a degree* is the following:
and date_expiry between curdate()- interval 1 month and curdate()+ interval 3 month
However the issue many may have picked up on is that the above query gathers data from today 1 month previous (10/11/2019) and 3 months from today (10/02/2020).
So I've been searching around and the closest I've got was this:
and month(date_expiry) = month(current_date- interval 1 month ) and year(date_expiry)= year(curdate())
This works perfectly for collecting everything in the previous month (01/11/2019-31/11/2019) but I somehow need to add something similar to gather data data for the advanced months.
Help please!
The curdate() suggests MySQL. You can handle full dates as:
where date_expiry >= (curdate() - interval (1 - day(curdate())) day) - interval 1 month and
date_expiry < (curdate() - interval (1 - day(curdate())) day) + interval 1 month
This is convenient because it is index-friendly.
Try DATE_SUB for substraction and DATE_ADD for 2 months advanced
cek this query is this the day you want to cek?
you can change the interval if you want and change the NOW() with your custom date yourself.
to learn about interval check this link
SELECT DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AS lastdaymonthbefore,
DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH) AS 2monthAdvanceFromToday
so you can edit your query like this
AND date_expiry BETWEEN DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AND DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH)

How to query for the first date of a week on the PREVIOUS year

Wracking my brain trying to figure this out, and I can't seem to find any existing threads that help.
Simply, I'd like to find the first day of the week (as a date) but one year ago, for any given date. Our calendar week starts on Sunday.
Here's a snap of the table I have at my disposal
Any help is greatly appreciated!
Thanks!
See this. How do I get the first day of the week of a date in mysql?
You can get your required result this way:
mydate - INTERVAL 1 YEAR + INTERVAL 1-DAYOFWEEK(mydate - INTERVAL 1 YEAR) DAY
Explanation:
mydate - INTERVAL 1 YEAR
gives you the date a year before mydate.
anyday + INTERVAL 1-DAYOFWEEK(anyday) DAY
gives you the Sunday beginning the week of anyday.
Similarly you can get the first day of the month of anyday like this:
LAST_DAY(anyday) + INTERVAL 1 DAY - INTERVAL 1 MONTH
Some people call this week- and month- truncation.
You can use datesub() to subtract one year from today (curdate()). Get the weekday with weekday(). It returns a number from 0 to 6 where 0 is Monday. Subtract that many days plus one, as your first weekday is Sunday not Monday.
date_sub(date_sub(curdate(),
INTERVAL 1 YEAR),
INTERVAL weekday(date_sub(curdate(),
INTERVAL 1 YEAR)) + 1 DAY)
Here's a function to do it:
CREATE FUNCTION `SUNDAY`(indate date) RETURNS date
NO SQL
BEGIN
declare prevyear date;
set prevyear = indate - interval 1 year;
return prevyear - weekday(prevyear) - interval 1 day;
END

select specific time from previous day

I basically want to select data between 7pm the previous day and NOW(). I'm not sure the best practice or most efficient way to do this on an automated report generated by a query I could write.
SELECT * FROM table WHERE timestamp BETWEEN curdate() - INTERVAL 1 DAY AND
NOW()
How do I get the curdate() - interval 1 day to start at 7pm of the previous day?
curdate() returns just the date, e.g. 2018-03-21. Time in this case is omitted, but would be 00:00:00. To start at 07:00 pm just add another 19 hours like this:
select curdate() - INTERVAL 1 DAY + INTERVAL 19 HOUR;

mySQL - Whole of last month

I am trying to check for all records that occurred last month using the following statement.
Select * from statistics
where statistics_date
BETWEEN date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH )
However, the selection does not include the last day. What I want is from the first second of the month until the last second of the month.
BETWEEN is notoriously bad for date and timestamp work because it gets the end date wrong.
Here's what you need:
First, let's compute the first day of the present month. You had that exactly right.
DATE_FORMAT(NOW(),'%Y-%m-01')
Next, let's compute the first day of last month:
DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 1 MONTH
Now, we select the records that lie in the interval.
Select *
from statistics
where statistics_date >= DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 1 MONTH
and statistics_date < DATE_FORMAT(NOW(),'%Y-%m-01')
Do you see how the beginning of the date range is chosen with >= and the end with <? Do you see how I used the first day of the present month for the end of the date range? Those things are important, because timestamps can have days and times in them. Consider the timestamp '2013-01-31 23:58'. It happens to be after '2012-01-31' so between won't catch it.
where MONTH(statistics_date) = MONTH(NOW()) - 1