Using FLOOR() in WHERE clause in MySQL - 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.

Related

Why doesn't this date query work, but a similar one does?

I'm building a query to determine how many workdays have been completed. I've already figured out the opposite, which is for days remaining. The queries are identical save for the > and < operators, yet the < operator does not work with the "wpdatatables" plugin I'm using. I know I may need to speak with their support about it but I figured I'd ask in case anyone has a clue:
SELECT count(calendar.date) AS Workday FROM calendar
WHERE calendar.is_weekday = 1 AND calendar.is_holiday != 1
AND year(calendar.date) = year (curdate())
AND month(calendar.date) = month (curdate()) AND day(calendar.date) <= day(curdate())
Seems simple enough, but the plugin keeps giving me an error saying it's not finding any data, whereas both queries work just fine in Sequel Pro. Do you think it could just be the plugin? Or perhaps there's another way to build the query.

TIMESTAMPDIFF giving unexpected result

Why is
TIMESTAMPDIFF(MONTH, '2015-12-25', '2016-02-24')
giving me 1? I would expect 2016-01-25 to be 1.
My guess is that it is actually returning something like 1.999 months and it is being simply rounded down.
How can I work around this? Or is there another function to use.
I tried PERIOD_DIFF but it does not take into account days
PERIOD_DIFF(DATE_FORMAT('2016-02-24','%Y%m'),DATE_FORMAT('2015-12-25','%Y%m'))
According to the documentation, the unit for the result is an integer, in your case it will return the whole number of months between the two dates, which is one (as the second month is not yet completed).

Why doesn't datediff for week work?

This was a bug that took awhile to find. I post it here to save someone else this mistake.
(It's extremely simple once it is found, but it is any easy -- and costly -- mistake to make.)
Why does select datediff(w, getdate()-7, getdate()) give 7 instead of 1?
The w is interpreted as days. It is never mentioned here, but inexplicably it seems to mean days. (I don't know why this doesn't give an error, as datediff(foo, getdate()-7, getdate()) does...)
The correct unit for week is ww or wk.
Edit: #Lamak says that w is weekday instead of day. There is nothing (I can find) documented about w, but this may be correct.

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.

timediff is greater than 2 minutes

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'