using CASE expression in the WHERE clause and BETWEEN operator - mysql

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

Related

DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)

DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)- I have this in mySQL, and I'm taking it into BigQuery. BigQuery does not have Last_Day function.
This getting the first of the month by calculating the last_day of the month before and adding one day.
Below is for BigQuery Standard SQL
#standardSQL
SELECT DATE_SUB(DATE_SUB(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 MONTH), INTERVAL 1 DAY)
if you run it today (2019-04-25) the output is
Row f0_
1 2019-02-28
Not sure what exactly you your target - I think below option is better represent your mysql version
#standardSQL
SELECT DATE_SUB(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 MONTH)
with result
Row f0_
1 2019-03-01
You could use DATE_FORMAT(date ,'%Y-%m-01')
DATE_ADD( DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 MONTH) ,'%Y-%m-01'), INTERVAL 1 DAY)

mysql where date is before now + 2 weeks

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 -----------^

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)

Grouping date of births in 2 month periods

I'm running a simple query that's returning the number of children turning 2 (birthday last month or next month). The following query returns a single row with count:
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 23 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 25 MONTH)
I'd like to be able to build on this query and return multiple count rows, each for a 2 month period so that I can predict 2nd birthdays moving forward.
I could do something like this:
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 23 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 25 MONTH)
UNION
SELECT COUNT(*)
FROM table AS t
-- Child turning 2
AND t.dob <= DATE_SUB(NOW(), INTERVAL 21 MONTH)
AND t.dob >= DATE_SUB(NOW(), INTERVAL 23 MONTH)
UNION...
But this is stupidly inefficient and very clumsy.
As an output I'd like to see something like this:
count |date range
---------------------------------
327 |2012-03-01 - 2012-04-31
---------------------------------
532 |2012-05-01 - 2012-06-31
I think I need to do something with GROUP BY but am unsure about how to go about this.
Many thanks.
select
case
when t.dob between date_sub(now(), interval 25 month) and date_sub(now(), interval 23 month) then 'between -25 and -23 month'
when t.dob between date_sub(now(), interval 22 month) and date_sub(now(), interval 20 month) then 'between -22 and -20 month'
when t.dob between date_sub(now(), interval 19 month) and date_sub(now(), interval 17 month) then 'between -19 and -17 month'
when t.dob between date_sub(now(), interval 16 month) and date_sub(now(), interval 14 month) then 'between -16 and -14 month'
end as my_ranges,
count(*)
from
t
group by my_ranges

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)