What time does it signify? - mysql

I created a table with one attribute tt and inserted a value into it.
CREATE TABLE tt(tm TIME);
INSERT INTO tt VALUES(2342342);
On executing the select command the result shown is in the form :
234:23:42
What time does this signify ?

MySQL retrieves and displays TIME values in HH:MM:SS format or HHH:MM:SS format for large hour values. The reason why it can have large values is because it can also represent an interval between two events (which may span over multiple days for example, or even be negative).
H stands for hour, M for minute, and S for second.
So, when you insert 2342342 it becomes 234:23:42 representing 234 hours, 23 minutes, and 42 seconds.
Reference

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from
'-838:59:59' to '838:59:59'. The hours part may be so large because
the TIME type can be used not only to represent a time of day (which
must be less than 24 hours), but also elapsed time or a time interval
between two events (which may be much greater than 24 hours, or even
negative).
http://dev.mysql.com/doc/refman/5.0/en/time.html
MySQL recognizes TIME values in these formats:
As a string in 'D HH:MM:SS' format. You can also use one of the
following “relaxed” syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH',
or 'SS'. Here D represents days and can have a value from 0 to 34.
As a string with no delimiters in 'HHMMSS' format, provided that it
makes sense as a time. For example, '101112' is understood as
'10:11:12', but '109712' is illegal (it has a nonsensical minute part)
and becomes '00:00:00'.
As a number in HHMMSS format, provided that it makes sense as a time.
For example, 101112 is understood as '10:11:12'. The following
alternative formats are also understood: SS, MMSS, or HHMMSS.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-literals.html

Related

Script to substract date time format to get result in hours

I have two cell that have time and date in this format "9/12/2021 10:41:571".
I need help with writing a function that will return the difference of both the timestamp in hours
As long as your date/times are formatted as numbers rather than text, you could subtract them from each other then multiply by 24:
=(A2-A1)*24
If the number of hours does not exceed 24, you can also use the following formulae:
=HOUR(A3-A2)
=MINUTE(A3-A2)
=SECOND(A3-A2)
=CONCATENATE(B2," hour/s ",C2," minute/s, ",D2," second/s")
Therefore, assuming that your two dates are located in the A2 and A3 cells, the formulae above will calculate the difference between the two returning the hours, minutes and seconds.
Spreadsheet
Reference
HOUR;
MINUTE;
SECOND;
CONCATENATE.

Interesting SSRS parameter discovery when using subscriptions

I have recently been putting together an SSRS report that will run every 15 minutes for the previous 15 minute 'chunk' of time. In essence a very straight forward and simple report that will run via an automated subscription.
I was using Microsoft SQL Server 12 Report Builder Version 3.
I was alerted to an issue with the output csv when my recipient reported being sent blank files, most odd considering the report generated as expected when run manually.
Long story short, it was the expressions I was using to generate the From and To dates. Manual runs produced data, subscription runs did not.
Original parameters
FromDate
dateadd(DateInterval.Second, (second(now()) + 900) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
ToDate
dateadd(DateInterval.Second, (second(now()) + 1) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
New Parameters
FromDate
dateadd(DateInterval.Minute, -15, dateadd(DateInterval.Minute, cint(datediff(DateInterval.Minute,today(),now()) / 15) * 15, today()))
ToDate
dateadd(DateInterval.Second, 899,Parameters!FromDate.Value)
Thought I would post this here for two reasons
Theories as to why
It might help someone in the future
Your original parameters take Now and subtract the minutes to arrive at a 15-minutes time, then they take the seconds of another Now (which is later and could be in the next second or even minute) and subtract that value to arrive at a 0-second time (or a 59-second time). This could already cause a problem when there is a change of seconds between the first and the second Now, which isn't very unikely, as on my test system there were 0.59 seconds between the two evaluations of Now in the FromDate parameter. Also, the Now value is more accurate than just a second, and your formula does not respect that. Therefore, if the records you are trying to process in your report happen to have a time of exactly a quarter of an hour, the first parameter is for sure greater (by maybe 0.01 second) and so the record is ignored.
Your formula for the "new parameters" does not depend on the seconds of Now() and will always return a time with no fraction of a second, so I guess that that's what makes the difference.
The expression for the FromDate could be simplified a little:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15-1))
If you do not plan to run the report very short before midnight, there should not be a problem caused by a change of the day during the evaluation of Today and Now, and you could calculate the second parameter in a similar way, independently from the first one:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15)-1)
The original parameter values were being calculated individually which means they would each have slightly different values for Now(). I know this is a long shot, but it's a theory. If the subscription job fired off a fraction of a second before a 15 minute interval, it's possible that the ToDate returned just before the FromDate. This would result in an invalid date range.
With the new expressions, the ToDate is referencing the FromDate which forces them to be calculated in sequence, not in parallel. Not to mention you're adding to the FromDate which also forces the date range to have a consistent length. However, you may still run into a case where you get the same report twice if the FromDate is calculated on the wrong side of a 15 minute cutoff.
One way to test/avoid this issue would be to offset the subscription time so that it doesn't actually try to fire at the exact 15 minute cutoffs. For example, you could have it scheduled to go off 1 minute afterwards.

How can I get Mysql's DATE_FORMAT() to display hours over 24?

I'm using DATE_FORMAT(time, '%H:%i') to get the time nicely formatted in HH:MM in a query, my problem however is that if the time goes over 24 hours it adds a day and the hour count will start again from zero, this way I can't display times like 40:00.
I have looked at the documentation here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
But there doesn't seem to be an obvious way to display hours over 24, the only choices seem to be 00..23 and 01..12, I'd be happy with something like 01..99 instead, is there a way to do this?
Try using TIME_FORMAT() instead of DATE_FORMAT(). TIME_FORMAT()'s documentation says that
If the time value contains an hour part that is greater than 23, the %H and %k hour format specifiers produce a value larger than the usual range of 0..23. The other hour format specifiers produce the hour value modulo 12

How to subtract certain minuted from a DateTime in SPSS

I have timestamps in a column which I have imported in SPSS. Example, 7/6/2011 2:21 in a column called 'Observation'
This is in the string format. Now I also have timezone corrections for these data. So, -60 would mean subtract 60 minutes from this date.
How would I do this in SPSS syntax?
There are native date formats in SPSS, but unfortunately it does not appear that any cover the example you posted. I would parse the beginning of the string field to get the mm/dd/yyyy and the hh:mm part seperate, convert those into their representative time formats, and then do the time calculations.
For an example
data list fixed / observation (A25).
begin data
7/6/2011 2:21
10/11/2011 15:42
07/06/2011 02:21
3/15/2011 0:21
end data.
*getting the data part, assuming the space will always delimit the two parts.
compute #space = char.index(observation," ").
string date (A10).
compute date = char.substr(observation,1,#space-1).
*getting the time part.
string time (A5).
compute time = char.substr(observation,#space+1,5).
execute.
*now converting them into date formats.
alter type date (A10 = ADATE10).
alter type time (A5 = TIME5).
*you should check these carefully to make sure they were converted correctly.
*now making one time variable.
compute date_time = date + time.
formats date_time (DATETIME17).
execute.
*now it is just as simple as subtracting the specified value.
compute date_time_adj = DATESUM(date_time,-60,"minutes").
execute.
formats date_time_adj (DATETIME17).

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.