timediff is greater than 2 minutes - mysql

I am trying to write a query where I can eliminate a timediff of less than 2 minutes. I have tried variations on the following which returns no results
timediff(sessions.producer_on,sessions.producer_off)>'00:02:00'
the timediff without the > works fine and returns all results - I am having difficulty with the >00:02:00 condition. Can anyone help - many thanks

You need to extract the minute from the time then compare it.
minute(timediff(sessions.producer_on,sessions.producer_off)) > 2 AND
hour(timediff(sessions.producer_on,sessions.producer_off)) = 0
Also it may be necessary to make sure that the hour is 0 since only when the hour is zero does the minute actually matter.

This is probably the "old way" but it eliminates the need to check hours, days, etc.
UNIX_TIMESTAMP(fieldOne) - UNIX_TIMESTAMP(fieldTwo) < 120
You can also use NOW() in place of a field name.

Change it to
timediff(sessions.producer_on,sessions.producer_off) > TIME('00:02:00')
and it should work.

SELECT * FROM e_email_otp WHERE TIMEDIFF('2019-01-11 10-46-19',`2019-01-11 10-45-19`) <'00:02:00.000000'

Related

Using FLOOR() in WHERE clause in MySQL

Is it possible to use FLOOR() in a WHERE clause, and if so, could someone tell me why the below code is wrong?
I have understood a lot from research on what it is and how to use it, but I cannot find anything which could fix this error.
select * from medications
where floor(datediff('d', date(update_time) – date(insert_time))) <=1
My intention is to pull all records which have been amended in the last X number of days, where X <= 1 in this case.
Having readdressed it and considered people's comments below, I have decided to just use the code below. I hadn't really understood FLOOR() well enough to use it. The one below works for me.
m.updt_time >= (DATE_SUB(CURDATE(), INTERVAL 10 day))
It substitutes the number of days for a Crystal Reports parameter.
Having readdressed it and considered people's comments below, I have decided to just use the code below. I hadn't really understood FLOOR() well enough to use it. The one below works for me.
m.updt_time >= (DATE_SUB(CURDATE(), INTERVAL 10 day))
I substitute the number of days for a Crystal Reports parameter.

MYSQL time diff

I'm trying to calculate time diff between two time fields. Because the fields are just time with no date, I can't use timestampdiff, so I'm using timediff() or subtime(). The only problem with those is when the second time is less than the first time, it returns a negative timediff for me. I understand it's taking the times as the same day times, but is there any way to get a behavior where it always calculates the time forward? For example, I have 23:00:00 and 07:00:00.
timediff('07:00:00','23:00:00') will return a negative value, because 23:00 is greater than 07:00, but I want to get 8 hours as the return. Is there any way I can do that?
You can achieve that with an if statement:
select if(dateA > dateB,
timediff(dateA, dateB),
addtime(timediff(dateA, dateB), '24:00:00.000000'))
This way, if the first date is smaller than the second, you add 24 hours to the difference.

MySQL - Multiplying Time Type Values - Some return 00:00:00

I'm trying to multiplay a field of type time. Sometimes it works and sometimes it doesn't. This is in phpmyadmin with MySQL.
Things I have tried already:
I have tried googling 'sql multiplying time type returns 00:00:00'
but have no luck with that or similar searches.
I ran a python script that pulled all these times and multiplied them
by 6 and 11 and all of them worked - there were no 00:00:00.
I have also tried doing it individually on a row, and it didn't work
for the same ones.
Below are pictures before and after I run this query:
UPDATE journeys, travel_type
SET journeys.jo_duration = journeys.jo_duration * travel_type.tt_duration_multiplier
WHERE journeys.jo_type = travel_type.tt_id
I get no errors.
Some probably helpful information:
Type 1 means 11 - so I want the value in jo_duration to be multiplied by 11. Type 3 means 6 (same principle). 2 means 1 (so nothing changes).
There are more rows in this table that have this problem but I didn't want to screenshot the whole table. I thought this should be enough. There are 70 total rows. When I run the above query it says '47 rows affected' (when all rows should technically be affected I think?). I'm not sure if the remaining aren't affected because they're type 2 or if they're the ones being turned to 00:00:00 (or both)?
If you need more information, feel free to ask!
As you can see below, rows with jo_id; 1, 3, 4, 6 are 00:00:00. But others multiply correctly?
Does anyone know why this is? And how I can prevent it?
Sorry for the long post and I hope my problem has made sense!
Thank you!
Before:
After:
You can't directly multiply number to a time datatype.
Use function time_to_sec and sec_to_time:
update journeys, travel_type
set journeys.jo_duration = sec_to_time(
time_to_sec(journeys.jo_duration)
* travel_type.tt_duration_multiplier
mod 86400)
where journeys.jo_type = travel_type.tt_id
mod 86400 is there to remove the day part if the time becomes more than a day

How can I tell how many minutes have passed between Now() and 7am that day?

Basically I just need to know how much time has passed from a certain time that day till Now() this will be run on a timer throughout the day and used to determine when something should be run (this might seem odd but there is logic behind it).
The issue with the code below is that it gives me a very high negative number. I can only assume that this is caused from the TimeSerial not actually containing a date and only the time so it throws everything off.
Can anyone point me in the direction of a way to do what I want? I am certain that the answer is something super simple that I am missing but I haven't been able to find it.
DateDiff("n",Now(),TimeSerial(07,0,0))
You want the number of minutes from 7 AM until now. Your DateDiff had those two swapped around and that's why you got a negative value.
The reason the magnitude of that number was so large is you were asking for the difference between 07:00 on Dec 30 1899 and today. This is what that TimeSerial expression gives you ...
? Format(TimeSerial(07,0,0), "mmm d yyyy, hh:nn:ss")
Dec 30 1899, 07:00:00
I think this is what you want instead ...
DateDiff("n", Date + #07:00#, Now)

Get the next xx:30 time (the next half-past hour) after a specified time

I'm trying to find a MySQL query which will obtain the time that is the next half-past-the-hour from a specified datetime. I explicitly mean the next half-past-the-hour, not the next half-hourly point.
So for instance:
If the datetime was "2009-10-27
08:15:24", the answer would be
"2009-10-27 08:30:00"
If the
datetime was "2009-10-27 08:49:02",
the answer would be "2009-10-27
09:30:00".
I came across this page which refers to SQL Server, and towards the end of that thread there is a similar sort of problem. But it's not quite the same, and it relies on a function that MySQL doesn't have.
Here is a fuller list of examples and expected return values:
2009-10-27 08:15:24 should return 2009-10-27 08:30:00
2009-10-27 08:49:02 should return 2009-10-27 09:30:00
2009-10-27 23:49:10 should return 2009-10-28 00:30:00
2009-10-27 10:30:00(.000001) should return 2009-10-27 11:30:00
(Note how, in the fourth example, because the exact half-past (10:30:00.0000000) has already gone, the next half-past-the-hour point is found.)
I tried using this kind of thing:
SELECT IF( (MINUTE(NOW()) < 30), HOUR(NOW()), (HOUR(NOW()) + 1) )
(after which addition of a CONCATed string would take place), but it would fail because of the changeover to another day, and it feels inherently 'hacky'.
Can anyone suggest a suitable sort of algorithm? I wouldn't expect a full answer (though that would be nice!), but suggestions as to the kind of algorithm would be helpful. I've been drawing over bits of paper for two hours now! I have a hunch that using modulo might be useful but I'm not sufficiently familiar with using it.
The answer will be fed to a PHP class later, but I'd rather implement this at SQL level if possible, as the rest of query also performs other date comparison functions efficiently.
This is a little messy, but works:
select from_unixtime( floor((unix_timestamp(MY_DATE)+(30*60))/(60*60))*(60*60) + (30*60) )
It pushes the time forward 30 minutes, then truncates to the top of the hour, then adds 30 minutes to it. Because it's working unix timestamps (seconds since 1970), you don't have to worry about the boundaries of days, months, years, etc.
I can't help but notice that this would be much easier at the PHP level :-) That said, here's what you can do:
Add 30 minutes to your datetime using DATE_ADD(); this will move to the next hour if it's already past half-hour
Create a new datetime value by extracting date / hour and hard coding minutes / seconds. CONVERT(), ADDTIME() and MAKETIME() all help.
The end result is:
select ADDTIME(
CONVERT(DATE(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), DATETIME), # date part
MAKETIME(HOUR(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), 30, 0) # hour + :30:00
) from ...
Use the MAKETIME(hour,minute,second) function to construct the desired value.