Mysql DateTime compare - mysql

Hi I have a table of the following 2 records:
descript | start | end
test 1 | 2011-07-18 14:30:00 | 2011-07-18 17:00:00
test 2 | 2011-07-18 00:00:00 | 2011-07-19 00:00:00
When I tried to do a select, I can't seems to retrieve the 2nd result (test 2) which seems like clearly it is dated 19th of July.
SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND end <= "2011-07-18 23:59:59";
Would appreciate any advise.

"2011-07-19 00:00:00" is MORE than "2011-07-18 23:59:59"
By your condition it should be less, so your query does not match test 2.
Your SQL query should be:
SELECT * FROM event
WHERE start >= "2011-07-18 00:00:00"
AND end <= "2011-07-19 00:00:00";

You can just do this :)
SELECT * FROM event
WHERE start BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
AND end BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
This results in the times in between the range you specified for start AND end

you just need to exclude column end, like
SELECT * FROM event
WHERE start>="2011-07-18 00:00:00" AND start<="2011-07-18 23:59:59";

Related

Select where datetime start greater than and end less than

I have a table with start and end field with values such as:
start | end
2021-09-24 17:00:00 | 2021-10-01 08:00:00
I have gone foggy headed and can't figure out why this statement results in nothing:
SELECT *
FROM `oncall`
WHERE `start` >= '2021-10-01 02:00:00'
AND `end` <= '2021-10-01 08:00:00'
although the date range in my example row contains the above range.
I am trying to find any row that overlaps the start and end of the values in the database.
Looking at the entry in your database and the query you're using, it seems like you're actually looking for a date interval overlap query i.e. a query that checks if [2021-10-01 02:00:00, 2021-10-01 08:00:00) overlaps [2021-09-24 17:00:00, 2021-10-01 08:00:00) somehow.
SELECT *
FROM `oncall`
WHERE #d2 > `start` AND `end` > #d1
-- replace #d1 and #d2 with actual values
Note that this query works for all kinds of overlap. For example if you have this pair of dates in your database:
2021-10-11 | 2021-10-15
then all of these input pairs will match:
2021-10-01 | 2021-10-12
2021-10-14 | 2021-10-20
2021-10-01 | 2021-10-20
2021-10-11 | 2021-10-14

MySQL display a record for On Call shift

I am trying to get a specific record to show that falls between a start date and end date at a specific time.
Here is what I have in MySQL:
SELECT *
FROM OnCallCalendar
WHERE
CallDay <= CURDATE()
AND CallEnd >= CURDATE()
AND callTime < CURTIME()
So a call shift would start on 9/27/2014 at 7:00am and run to 9/28/2014 at 7:00am. The entries in the database would be something like this:
doctor | CallDay | CallEnd | callTime
smith 9/27/2014 9/28/2014 07:00:00
jones 9/28/2014 9/29/2014 07:00:00
SELECT *
FROM OnCallCalendar
WHERE
NOW() BETWEEN CONCAT(CallDay, ' ', CallTime) AND Concat(CallEnd, ' ', CallTime)

MySQL date_add behaviour

I've written some SQL to give me a range of dates between two times like so:
select date_add(x.min_date, interval ((t500.id-1) * 30) minute) period
from (
select '2013-08-05T23:00' as min_date, '2013-08-06T01:00' as max_date
) x,
t500
where date_add(x.min_date, interval ((t500.id-1) * 30) minute) <= x.max_date);
Where T500 is a trivial table with column id of 1 to 500 I use for simulating a loop.
Now I expect this to return:
2013-08-05 23:00:00
2013-08-05 23:30:00
2013-08-06 00:00:00
2013-08-06 00:30:00
2013-08-06 01:00:00
and finish there. But instead it carries on until 2013-08-06 23:30:00. I tried different max dates and it always returns dates to the end of the day. Could someone explain what's happening and how to make it stop when I want?
First thing that comes to mind would be casting your date strings into a date format instead of a string for example:
cast('2013-08-05T23:00' as smalldatetime)

Mysql date less than week operation

Suppose you have two datetime values, date_a and date_b
What expression evaluates to true if date_a occurs within or less than the calendar week of date_b?
Just use MySQL's week() function.
Example:
mysql> create table daten (a date, b date);
mysql> insert into daten values ('2011-04-14', '2011-04-12');
mysql> insert into daten values ('2011-04-14', '2011-04-22');
mysql> select * from daten where (week(a)=week(b) and year(a)=year(b)) or a<b;
+------------+------------+
| a | b |
+------------+------------+
| 2011-04-14 | 2011-04-12 |
+------------+------------+
This will give you all records with a in the same or an earlier calendar week than b. Note that week(a)<=week(b) would not work, because weeks start again each year.
Note: There is more than one convention for counting calendar weeks (week starts on Sunday or on Monday, different start of first week of the year). You can pass an optional second parameter "mode" to week() to tell it which convention to use; see the docs. Of course for this problem only the start day of the week matters, not what the first week of the year is.
A more brute force method:
Select ...
From DateValues
Where date_a >= Date_Add( date_b, Interval -DayOfWeek( date_b ) + 1 Day )
And date_a <= Date_Add( date_b, Interval -DayOfWeek( date_b ) + 7 Day )

mysql query problem

id url start end
1 http://yahoo.com 2010-10-17 2010-10-10
2 http://google.com 2010-10-15 2010-12-11
3 http://espan.com 2010-10-20 2011-01-20
4 http://espan.com 2010-10-01 2011-01-01
if Today is 2010-10-16..
how can I get results work in today.
2 http://google.com 2010-10-15 2010-12-11
4 http://espan.com 2010-10-01 2011-01-01
SELECT id, url, start, end
FROM Your_Table
WHERE 2010-10-16 BETWEEN start AND end
Replace 2010-10-16 with CURRENT_TIMESTAMP or equivalent in MySQL
SELECT * FROM your_table WHERE CURDATE() >= start AND CURDATE() <= end
Something like this?
SELECT *
FROM that_table
WHERE CURRENT_TIMESTAMP BETWEEN start AND end
Note that:
in the above example, both dates are "inclusive"
in the above example, CURRENT_TIMESTAMP includes the "time" part as well