MySQL Convert 48 Hours into 48:00:00 - mysql

I have a regular DATETIME row, eg: 2012-06-19 13:56:56
I am running the following to see how much the time difference is:
DATEDIFF(end_time, NOW()) * 24
It returns 48
Edit: How do I get the Minutes/Seconds? I have tried UNIXTIME(field) - UNIXTIME(NOW()) but i cant get beyond it.
Im trying to convert this into 48:00:00 (Or however that timestamp works)
I keep looking up time functions but they have to do with EXTRACT, and Im not sure thats the way to go about it.

Since datediff only ever returns a difference in days, you might as well just use a string operation to do your formating:
SELECT CONCAT(DATEDIFF(end_time, NOW()) * 24), ':00:00')

Related

Convertint millisecond to date

I have a column in a table that contains time in milliseconds (2147483647), or so I have told. I am trying to convert it into the actual time (human time), but haven't been so lucky. I have tried everything I thought was going to be of help, though none of the things I have found on google, and on here, have been helpful.
SELECT arrivalTime, FROM_UNIXTIME(uploadTime), "; //UNIX_TIMESTAMP(uploadTime) * 1000 // UNIX_TIMESTAMP() * 1000
The arrivalTime is uploaded in a different time format but I already have that working. I also have a different table using a different time format, which is also working but I am including it here on my post just in case it can be used as a reference or someone might find it useful in their code.
Date_Add('1970-01-01 0:0:0', INTERVAL(uploadTime/1000 - (timeZoneOffset*60)) SECOND) AS uploadTime
Any help or suggestion would be GREATLY appreciated!!!
PS: the current query gives me this 2038-01-18 22:14:07 as the time, which is obviously wrong. And I have also tried this
FROM_UNIXTIME(uploadTime/1000);
but didn't also do what I wanted
PSS: Okay, after asking around, I found out that this 2147483647 is from Android getTimeInMillis from calendar.API. Hope that helps anyone?
I would do something like this in SQL Server
DateAdd(Second, (2147483647/1000), CAST('1970-01-01 00:00:00' AS datetime)) AT TIME ZONE 'Central European Standard Time' AS uploadTime
This gives me the output:
After your update that the milliseconds comes from Android getTimeInMillis():
Using this milliseconds in my function above:
DateAdd(Second, (1566302250040/1000), CAST('1970-01-01 00:00:00' AS datetime)) AT TIME ZONE 'Central European Standard Time'
results into 2019-08-20 11:57:30.000 +02:00 which seems to be right.

DATE_ADD Returning NULL on production server, working in development

Development is localhost running version 5.6.16, production is 5.1.73-cll
The DATE_ADD of this query returns NULL on production, but in development is does exactly what I want it to(adds 90 minutes to the game_time column), The game_time column is a string that contains time in the following format: '21:00'.
This is the query:
SELECT TIME(game_time),
DATE_ADD(TIME(game_time),
INTERVAL 90 MINUTE),
TIME(NOW())
FROM games
What is going on? What am i doing wrong?
I know time should be in a TIMESTAMP, or TIME, but I'm working on someone elses code, I didn't start this from scratch myself.
I've also just noticed that TIME() returns different things, in development, TIME('21:00') returns 21:00:00.000000, in production I only get 21:00:00
Managed to get around, not pretty, but it works.
SEC_TO_TIME(TIME_TO_SEC(TIME(game_time))+5400)
You better develop with the same version as the production server:
Your old version will convert your TIME value to a date and because it's an invalid date, it will get NULL, see manual chapter Conversion Between Date and Time Types
Here's the relevant part:
Before 5.6.4, MySQL converts a time value to a date or date-and-time
value by parsing the string value of the time as a date or
date-and-time. This is unlikely to be useful. For example, '23:12:31'
interpreted as a date becomes '2023-12-31'. Time values not valid as
dates become '0000-00-00' or NULL.
Edit:
To get a TIME value with the desired result, you could use ADDTIME.
This could be working:
SELECT TIME(game_time),
ADDTIME (TIME(CONCAT(CURDATE(), ' ', game_time))),
'01:30:00'),
TIME(NOW())
FROM games
untested, because I have no such old MySQL version anymore.
Try moving the conversion to time outside the DATE_ADD:-
SELECT TIME(game_time), TIME(DATE_ADD(game_time, INTERVAL 90 MINUTE)), TIME(NOW())
FROM games
DATE_ADD works on a DATE or DATETIME field, and as it is you are passing it a TIME field.

how to optimize Mysql query computing dates and min/max/average?

I've a table of measurements with timestamp. One data each 5 minutes.
I've created a view to extract average/min/max values for every 30minutes. Problem is I found this query very slow (+/- 5 s for total of 13290 rows... which is very few..
Any idea of optimisation ?
My code:
SELECT t_mesures.sonde_id AS sonde_id
,min(t_mesures.timestamp) AS start_period
,max(t_mesures.timestamp) AS end_period
,from_unixtime(
unix_timestamp(min(t_mesures.timestamp))+
floor( (unix_timestamp(max(t_mesures.timestamp))-unix_timestamp(min(t_mesures.timestamp)))/2)
) AS mid_period
,timediff(max(t_mesures.timestamp), min(t_mesures.timestamp)) AS dur_period
,avg(t_mesures.Mesure) AS avg_mesure
,min(t_mesures.Mesure) AS min_mesure
,max(t_mesures.Mesure) AS max_mesure
,count(t_mesures.Mesure) as nb_mesure
FROM t_mesures
GROUP BY t_mesures.sonde_id
,(floor((unix_timestamp(t_mesures.timestamp) / 1800)) * 1800)
As you've already noticed, calculating these things live tends to get pretty heavy.
The best way to make sure you don't have this problem is by simply not calculating it. In addition to storing the 5 minute interval data you should also store the 30 minutes min/max/avg/count data (and/or any other interval you're going to use).
Alternatively, you could also try something like Whisper, RRD or OpenTSDB

MySQL "date_sub(now(), INTERVAL X" Error Code: 1366 Incorrect string value: '\xEF\x80\xA6Top...' for column 's' at row 3

I have a problem here thats really puzzling me. I have a MySQL query, that works fine.. for the most part. In the query, Im looking at dates and date ranges from a table, and returning results that are X days old from today. It looks like this:
WHERE tickets.date_created > date_sub(now(), INTERVAL 30 DAY)
Under most conditions, the query works fine. However, if I change the interval to a number between 80 to 97 my script fails to execute. The error I get is:
Error Code: 1366 Incorrect string value: '\xEF\x80\xA6Top...' for column 's' at row 3
Also, tryin 3 months, rather than 90 days doesnt work. There appear to be other numbers in the 100's that dont work too, however, if I set something like 10000 days, all results return ok.
Is this something in my database? Is it my query? Has anyone seen this with a date interval? What am I doing wrong?
Any thoughts anyone has :)
Many thanks!
I've found the answer!
There is a function built into our MySQL to strip out HTML and other characters from a returned result. Whilst its not performing any action on the date area (or shouldn't be) as its in my select statement:
mycleanup (left(tickets_messages.message, 250))
It appears that if I change this to simply
left(tickets_messages.message, 250)
My query then works! Very odd, but I guess Ill have to find another way to work around stripping HTML and the like.

Difference in minutes from two time fields in MySQL

I set up my MySQL database with the field 'time'
It is not HH:MM in the traditional sense, it is the time an event occurred, so an event with the value of 5:45 occurred with 5 minutes 45 seconds left in a game. 12:25 occurred with 12 minutes and 25 seconds left, etc.
I would like to be able to find out the total time elapsed, so if I have an event that occurred at 12:25 and the next event occurred at 5:45 I want to be able to get the difference, which would be equal to 6:40. Then, I would like to express this as a decimal, in this case 6.67.
Is this possible?
For me this worked:
TIMESTAMPDIFF(MINUTE, T0.created, T0.modified)
Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
Just use
TIMEDIFF(fromtime, totime):
SELECT TIMEDIFF("12:25", "5:45");
If you need a decimal, use TIME_TO_SEC()/3600 (it assumes you passed it seconds, but the format you're storing the times in is vague and SQL probably will interpret them as HH:MM - you can fix this with CONCAT("00:",MinuteSecondField) maybe?) - then you can use TIME_TO_SEC()/60, which is more correct)
I needed similar. Two useful functions: TIME_TO_SEC and SUBTIME.
e.g. if your time fields were normal HH:MM times, then
SELECT (TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))/60 AS `minutes`
In your case, as I understand it, the times are backwards, ie. 12:00<6:00 so your end_time and start_time would need swapping.
If you wanted the output in HH:MM:SS you could do
SELECT SUBTIME(end_time, start_time)
I used UNIX_TIMESTAMP(event1)-UNIX_TIMESTAMP(event2), which gives you seconds between events. Divide it by 60.0 and you will get a decimal as per your requirement. This solution assumes, your columns are date-compatible values. It will work really fast if they are TIMESTAMPs.
UPDATE: This solution only works with timestamp data types, not time datatypes as in original question.