Save time duration of greater that 24 hours - mysql

I have a Database in 4th Dimension. It stores its time values in number format (seconds).
Now I have a Time duration of 29:30:00 ( 29 hours: 30 min : 00 Sec )
But this conflicts with the TIME rules of mySQL ( cant be > 23:59:59 )
I tried to convert this to number and save it, but I suspect it MySQL sees that it is a Time field and a number is not valid.
I was thinking about a format like DD:HH:MM:SS but im not sure if that is allowed.

There should be no problem storing time values greater than 24 hours in either 4D or mySQL.
4D:
A Time field, variable or expression can be in the range of 00:00:00 to 596,000:00:00
mySQL:
TIME values may range from '-838:59:59' to '838:59:59'.

Related

How to sum time interval stored as string in MySQL like 5 days 10:20:00

I need to write a select query to sum the time interval from MySQL table where the time interval is stored as text and in the format similar to following
10 days 3:28:31
In the PostgreSQL query we can simply use ::interval and it converts above to interval and we can use Sum method over it in PostgreSQL query but I am unable to do the same in MySQL. Any help would be appreciated.
MySQL does not have an interval data type. It does use the interval keyword -- which is a bit confusing. But that is a syntactic element, rather than a data type.
One thing you can do is use the time data type. This supports times up to 838 hours -- or about 35 days. That is enough for many purposes.
Otherwise, the recommendation is to use a single time unit and to store the value as a numeric quantity. For instance, "5 days 10:20:00" would be:
5.43055555556 days (5 + 10 / 24 + 20 / (24*60))
130.333333333 hours
7820 minutes

Selecting between two timestamps returns no data

I have two unix timestamps 65 seconds apart and i am trying to query mysql in this manner
This is 65 seconds apart 1504684252 + 65
SELECT ask FROM live_rates WHERE the_time BETWEEN
UNIX_TIMESTAMP(1504684252) AND UNIX_TIMESTAMP(1504684317)
SELECT ask FROM live_rates WHERE the_time BETWEEN
FROM_UNIXTIME(1504684252) AND FROM_UNIXTIME(1504684317)
In my table, there is an event starting at timestamp 1504684252 and ending 65 seconds later. Why is there no data returned by either of the queries?.
Your question doesn't really make it clear what data type the_time is - if it's a unix timestamp (i.e. an integer) already, just query it as an integer, given that you seem to know the unix timestamps of your date range:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
If your data in the_time is stored as a datetime, I'd say you should make sure that the server really is converting the values you've supplied to a datetime range that includes the relevant record:
SELECT ask FROM live_rates WHERE id = abcdef
SELECT FROM_UNIXTIME(1504684252), FROM_UNIXTIME(1504684317)
Run these two queries (with the id replaced) and eyeball the data. You'll find the reason, I'm sure - possibly something like a timezone issue, and your unix timestamps are translating to some time in UTC whereas your table data is showing some other time and you've hence mis-translated the time shown into a unix time.. Or possibly that it's an end date that has some time component outside the range of the unix times you specified even though the date part of it is correct.
It's hard to say for sure without a complete, verifiable example (a create table statement with inserted test data and a select demonstrating the problem. Try sqlfiddle.com)
ps; Don't use UNIX_TIMESTAMP() in the way you wrote here; it's not intended to have a unix timestamp passed into it. You either pass it no arguments (in which case it gives you the current datetime of the server clock, as a unix timestamp) or you pass it some datetime (and it will return you the equivalent unix timestamp of that datetime)
If the_time is a unix timestamp as opposed to a date i.e. 1504684252 instead of 2017-09-01 00:00:00, then you don't need FROM_UNIXTIME, which converts a unix timestamp to a date:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
UNIX_TIMESTAMP takes a date/time as parameter and returns an integer. When you're feeding it with an integer it returns 0 - so you're selecting between 0 and 0.
FROM_UNIXTIME takes an integer but it returns a date/time - so if the_time is an integer they're not compatible.

Find all TIME fields between 15-29 minutes with hour related to timezone

When saving TIME values into the MySQL database, our server automatically converts these dates to be formatted in UTC time, because of this we've stored our user's timezone offset in our database. Considering we had a simple database like composed of three fields, how would we go about getting the time within the allotted minute range with the appropriate hour.
id | offset | alertTime
---|--------|----------
1 | 360 | 4:22:38
2 | 420 | 3:28:41
In this table, user 1 has a 6 hour offset (CST) and user 2 has a 5 hours offset and a 7 hour offset (MDT) I need to find both of these user's (and any other users) that are all within the same hour after timezone calculation.
The conversion to these user's time is nearly the same, both users have an alert scheduled for 10PM 22Minuts nad 10PM 28 minutes, although different times are entered into the database due to UTC conversion.
Let's say I wanted to get all users who have an alert set for 10PM, between 10:16 and 10:29 CST regardless of their timezone.
Just to clarify, user's who set their alertTime in their own timezone, should be returned if their timezone converts to 10:16 - 10:29 CST. So, 9:16-9:29 MDT would also be returned in these results.
Hope that wasn't too confusing.
--
I'm using MariaDB through NodeJS if that matters.
Don't store them into a TIMESTAMP column; store them as DATETIME. That way, there is no offset during the insert, nor the select.
If you also need UTC, maybe you need two columns, one of each type?

Delete from database if time is greater than 4 hours

I want to delete all records in a database if the timestamp is older than 4 hours.
So my logic is to get the hour of the current time and get the hour of from the timestamp saved in the database and subtract to see if it is greater than 4. If it is greater than 4 than delete the records.
This is a code work in progress not really sure if it is correct.
DELETE FROM posts
WHERE id IN
(SELECT *
FROM posts
WHERE (HOUR(CURRENT_TIMESTAMP) - HOUR(time_published)) > 4)
)
if it makes a difference I am using MySQL.
Why not a simple
delete
from posts
where timestampdiff(hour, current_timestamp, time_published)>=4
Note that comparing the hour portions of date fields won't do what you expect. Consider comparing 21st Jan 1985 10:00 and 22nd Jan 1985 11:00. Your original condition would fail (1 hour), but it's actually 25 hours between them.
If you save the record at timestamp 1:00' and run this query at 5:59' nothing will be removed because the time difference is 4:59' and the hour component of that is 4! It might be what you want but that's way closer to 5 hours than 4.
Assuming that minutes are your smallest unit of precision, you might want to do something like
DELETE FROM posts
WHERE TIMESTAMPDIFF(MINUTE, CURRENT_TIMESTAMP, time_published) > 240
And use nested queries only when they are absolutely necessary; they possibly could slow your operations down drastically.

Convert timestamp to X days X hours X minutes ago

I've created a stored procedure in MySQL Server 5.1.
How can I convert a timestamp to a string that represents the days, hours, and minutes difference between that timestamp and now?
For example, if the timestamp is 5 hours and 3 minutes ago I'll get '5 hours 3 minutes ago'.
select date_format(timediff(current_timestamp,last_timestamp),
'%k hours, %i minutes, %s seconds ago');
If you want more luxury you can do something like:
select concat
(
if(date_format(timediff(ts1,ts2)'%k')='0'
,'',date_format(timediff(ts1,ts2)'%k hours'),
if(date_format(timediff(ts1,ts2)'%i')='0'
,'',date_format(timediff(ts1,ts2)'%k minutes'),
if(date_format(timediff(ts1,ts2)'%s')='0'
,'',date_format(timediff(ts1,ts2)'%k seconds')
)
Plus a few extra spaces here and there.
If one of the timestamps is null naturally the result will also be null, you'll have to make sure it is not, or use ifnull(`timestamp`,now()) to fix that.
See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Have a look at the MySQL reference page for date and time functions at
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Edit: Since I assume you are using Unix timestamps, the way to go is
FROM_UNIXTIME(timestamp, format)