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.
Related
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.
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.
I have noticed with one of my reports (SSRS), when I add weekdayname, that tomorrow's value appears.
I tested this by adding a textbox with =WeekdayName(weekday(Today())) in it. I have just run this (on a Monday) and it is saying Tuesday. So clearly it's one day out.
Does anyone know how I can go about rectifying this? I can get round it in reports by adding an expression but I suspect there's some deeper problem that I would like to rectify.
Any advice would be much appreciated.
Try specifying the first day of the week in the Weekday function to be determined by the system settings.
=WeekdayName(Weekday(Today(),FirstDayOfWeek.System))
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)
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.