I've a table in a db with some date field with format yyyy-mm-dd
I'm trying to perform a query that take just records with a interval of 3 month from today.
I've done like this
WHERE DATE_SUB(myTable.myField, INTERVAL 3 MONTH) = CURDATE()
and it works, but my second step is ignore years of my date field and from curdate().
I've tried EXTRACT or DATEFORMAT, but query doesn't work with those function.
How can I modify my query?
Thanks
The condition is wrong.
Try this instead:
...WHERE myTable.myField >= CURDATE() - INTERVAL 3 MONTH...
EDIT:
Based on your comment:
with my query i've got all record that have in dateField this date
'2016-12-07' (curdate() is today '2016-09-07') and it's fine. but i
want that query gives me also date that have 12 on month and 07 on
day, ignoring year. Eg. if i have '2016-12-07' and '2014-12-07', my
query must give me both records. it's a query that will run every day
...WHERE DATE_FORMAT(myTable.myField,'%m-%d') =
DATE_FORMAT((CURDATE() + INTERVAL 3 MONTH),'%m-%d')...
Use below condition which will find records with a interval of 3 month from today.
WHERE myTable.myField = DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
Related
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 do i extract all rows greater then 7 days of a start date?, I'm trying to use this query in MySQL. Below is my statement.
SELECT * from v_polygons a
INNER JOIN tblProjectData z
on z.Project_ID = a.Project_ID
WHERE DATE_ADD(z.FlyDate, INTERVAL 7 DAY) > NOW() + INTERVAL rge DAY
I have a start date z.FlyDate, So i give it +7 days, then i check to see if that date is greater then NOW()
is this correct or have i messed it up?
You can just do:
WHERE DATE_ADD(DATE(z.FlyDate), INTERVAL 7 DAY) < DATE(NOW());
This will ignore the time part. You can remove DATE function call if you want to consider the time as well.
I have a date field in the database table of this format 2012-02-1.i need to write 3 different queries:
a.) I need to retrieve all fields where date is between today and previous 5 days.
b.) I need to retrieve all fields where date is older than 5 days from today's date.
c.) I need to retrieve all fields where date between '5 days ago' to '30 days ago'
Can I use some inbuilt mysql function.
Manipulating the query below:
SELECT fields
FROM table
WHERE date >= CURDATE() - 5
or something like this
Or using a between clause. I am not getting the syntax correct.
SELECT p.status,p.downpayment_date,p.policy_id,i.id,i.policy_type,i.carrier,i.policy_number,i.client_id,c.id,c.client_name FROM pdp_payment AS p,pdp_policy_info AS i,pdp_client_info AS c WHERE p.policy_id=i.id AND i.client_id=c.id AND (((p.status='close pending') OR (p.status='Cancel')) AND (p.downpayment_date BETWEEN ((INTERVAL 5 DAY AND CURDATE()) - (INTERVAL 30 DAY AND CURDATE()))) )
Date between today and previous 5 days.
SELECT fields FROM table
WHERE date_field BETWEEN CURRENT_DATE - INTERVAL 5 DAY AND CURRENT_DATE
Date smaller than previous 5 days.
SELECT fields FROM table
WHERE date_field < CURRENT_DATE - INTERVAL 5 DAY
For all fields where date is between today and previous 5 days.
SELECT fields
FROM table
WHERE your_date_field_name BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()
You can work out other problems in a similar way
date is a keyword, so when you use it as a field name it MUST be enclosed in backticks ` otherwise you will get a parse error.
To get the range you want:
WHERE `date` BETWEEN DATE_ADD(NOW(),INTERVAL -30 DAY) AND DATE_ADD(NOW(),INTERVAL -5 DAY)
I'm using
SELECT * from tbl_name WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
to select data for specific days. The problem is that line gets data right before 3 days.
What to do so selected data to be period three days before till now ?
First your field should be of type datetime or date and then you can use a between clause
your_date_field BETWEEN now() - INTERVAL 72 HOURS AND now()
I need to get the result from the table, which the date should be difference of 5 from the current date.
ie., specific_date column is present in my table. The format of the date is YYYY-MM-DD.
I need the query something like,
SELECT * FROM `table_name` WHERE DATEDIFF(NOW(), specific_date) < 5
It looks like you are trying to do this:
WHERE specific_date < (NOW() + 5 days)
First of all, think carefully about the boundary cases. These boundary cases can bite your ankles in SQL. Do you actually want
WHERE specific_date <= (NOW() + 5 days)
Do your specific_date columns timestamps contain only days (that is dates with times equal to midnight) or do they contain dates and times? If you're going to get the results you want, you need to be careful about those details.
At any rate, try this:
WHERE specific_date <= DATE_ADD(NOW(), INTERVAL 5 DAY)
This is a good way to do such a lookup. The column value stands alone on one side of the inequality predicate (the <=) so mySQL can do an index range scan if you have an index on the column.
Date arithmetic in MySQL is quite flexible. You can do
NOW() + INTERVAL 10 SECOND
NOW() - INTERVAL 2 MINUTE
NOW() + INTERVAL 4 HOUR
CURDATE() - INTERVAL 1 WEEK /* midnight one week ago */
LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH /*first day of present month*/
NOW() - INTERVAL 1 QUARTER
CURDATE() - INTERVAL 5 YEAR
and so forth.
Have a look at the BETWEEN operator.
expr BETWEEN min AND max
Another way is to use the DATE_SUB operator
WHERE date_column > DATE_SUB(NOW(), INTERVAL 5 DAY)