mysql where date is before now + 2 weeks - mysql

How to get the rows out of a MySQL DB where the field date is before NOW + 2 weeks?
I have tried
WHERE date_ready < DATE_SUB(CURDATE(), INTERVAL 2 WEEK)
But that is not the rows returning that I expect.

Or even, now() minus 2 week,
where date_ready < (NOW() - INTERVAL 2 WEEK)
with just date
where date_ready < (CURDATE() - INTERVAL 2 WEEK)

You're querying dates that are before today minus two weeks, not plus. You should use date_add instead of date_sub:
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL 2 WEEK)
-- Here -----------^

try this:
WHERE date_ready < DATE_ADD(now(), INTERVAL 2 WEEK)
-- Here -----------^
and
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL + 14 Day)
-- Here -----------^

Related

SQL: Show "running" events

my script shows the events of the next 14 days:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
This works fine so far BUT at midnight all "running" (night)events are not displayed anymore.
So the query should output the events from the day before till 5 a.m.
This is my idea but it doesn't work:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 HOUR)
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)
Is it because 'eventdate' is a DATE-field?
Thanks!
It sounds like you want to subtract 5 hours before doing the comparison:
WHERE date_sub(eventdate, interval 5 hour) BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
Alternatively, you can add 5 hours on the other side:
where eventdate between now() and datea_add(now(), interval 14*24 + 5 hour)
This should do the job or not!?
WHERE
eventdate
BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 5 HOUR))
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)

How do I retrieve data for the previous month in SQL

I want to get data for the dates between 2015-05-01 and 2015-06-01 using SQL.
Please help me with the query.
The query I used is:
select *,count(id) as multiple_visitors
from table1
where id=123
and (date(server_time) between (CURDATE() - INTERVAL 31 DAY) AND CURDATE())
group by user_id having count(id)>1
You can do this with month() and year():
where month(server_time) = month(curdate() - interval 1 month) and
year(server_time) = year(curdate() - interval 1 month)
However, I recommend a slightly more complex expression:
where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) and
server_time < date_sub(curdate(), interval - day(curdate()) + 1 day)
The advantage is that there are no functions on server_time, so the database engine can use an index, if appropriate.
As a note: the expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on the first day of the month.
Try using "WHERE" with MONTH(date).
Like this:
SELECT * FROM Table
WHERE MONTH(date) = 1

Compare the last 24h with the same period a year ago in MySQL

I have a requirement to compare the last 24h with the same period a year ago.
If I do:
event_date < now() - INTERVAL 365 DAY and event_date > now() - INTERVAL 366 DAY
it does not take into consideration leap years.
The where clause you would need to create the condition for the records a year ago would be:
WHERE event_date >= DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 24 HOUR)
AND event_date <= DATE_SUB(NOW(), INTERVAL 1 YEAR)
By doing it this way you will not have to worry about the leap years.
To get the data for the current past 24 hours you would use:
WHERE event_date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
AND event_date <= NOW()
try
event_date < now() - INTERVAL 1 YEAR
AND event_date > now() - INTERVAL 1 YEAR - INTERVAL 1 DAY
or try
event_date BETWEEN (now() - INTERVAL 1 YEAR - INTERVAL 1 DAY)
AND (now() - INTERVAL 1 YEAR)

using CASE expression in the WHERE clause and BETWEEN operator

I'm trying to fetch some rows from my table based on some condition as follow:
SELECT * FROM MyTable WHERE Date BETWEEN
CASE dayofweek(curdate())
when 1 then curdate() AND adddate(curdate(), interval 6 day)
when 2 then subdate(curdate(), interval 1 day) AND adddate(curdate(), interval 5 day)
when 3 then subdate(curdate(), interval 2 day) AND adddate(curdate(), interval 4 day)
when 4 then subdate(curdate(), interval 3 day) AND adddate(curdate(), interval 3 day)
when 5 then subdate(curdate(), interval 4 day) AND adddate(curdate(), interval 2 day)
when 6 then subdate(curdate(), interval 5 day) AND adddate(curdate(), interval 1 day)
when 7 then subdate(curdate(), interval 6 day) AND curdate()
END
but for some reason it doesn't work. it gives me a syntax error instead. how should I accomplish something like this?
Try this instead,
SELECT *
FROM MyTable
WHERE 1 =
CASE dayofweek(curdate())
when 1 then Date BETWEEN curdate() AND adddate(curdate(), interval 6 day)
when 2 then Date BETWEEN subdate(curdate(), interval 1 day) AND adddate(curdate(), interval 5 day)
when 3 then Date BETWEEN subdate(curdate(), interval 2 day) AND adddate(curdate(), interval 4 day)
when 4 then Date BETWEEN subdate(curdate(), interval 3 day) AND adddate(curdate(), interval 3 day)
when 5 then Date BETWEEN subdate(curdate(), interval 4 day) AND adddate(curdate(), interval 2 day)
when 6 then Date BETWEEN subdate(curdate(), interval 5 day) AND adddate(curdate(), interval 1 day)
when 7 then Date BETWEEN subdate(curdate(), interval 6 day) AND curdate()
END
The CASE() statement ,in this scenario, will only return two possible values: 1 and 0.
A CASE returns a value, not an expression. You must repeat the CASE statement for each side of the BETWEEN:
SELECT * FROM MyTable
WHERE Date BETWEEN
CASE dayofweek(curdate())
when 1 then curdate()
when 2 then subdate(curdate(), interval 1 day)
... etc
END
AND -- this is the "AND" for the BETWEEN values
CASE dayofweek(curdate())
when 1 then adddate(curdate(), interval 6 day)
when 2 then adddate(curdate(), interval 5 day)
... etc
END

get 2 days ago query in mysql

So for getting last 24 hours query I use something like this
SELECT COUNT(*) AS cnt FROM `mytable` WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY)
where timestamp is a table field with timestamps.
but how can I get the interval between 2 days ago and yesterday.
So
today is 24 ian. I want a query between 22 ian (00:00am) and 23 ian (00:00am)
WHERE timestamp BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)
You can also try DATE_ADD with a minus interval ;)
WHERE timestamp BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL -2 DAY)
AND DATE_ADD(DATE(NOW()), INTERVAL -1 DAY)
Use Interval
WHERE `timestamp`
BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
If you want a query between 22 Jan (00:00 AM) and 22 Jan (11:59 PM)
where DATE(timestamp) = DATE_SUB(DATE(now()), INTERVAL 2 day);
Example:
timestamp = 2020-02-24 12:07:19 and Date(timestamp) is 2020-02-24 and now() output is current date with time when we use DATE(now()) then output is Date only,
DATE_SUB(DATE(now()), INTERVAL 2 day)
is will be 2 days ago.
Try BETWEEN::
SELECT
COUNT(*) AS cnt
FROM `mytable`
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY) and DATE_SUB(NOW(), INTERVAL 1 DAY)