I need to calculate the difference between two columns which are of type time. The fields are named start_time and end_time. I use the function timediff(s,e) and it works well but some times it doesn't. For example when end_time is 00:00 which is considered as 12 AM and start_time is 19:00 which is 7 PM the difference function shows 19 hours (actually I also select hour(timediff(s,e))) while I expect it to be 5 hours.
How can I fix this?
I tried this
case
when finish_time < start_time then TIMEDIFF(timestamp('2021:01:02',finish_time),timestamp('2021:01:01',start_time))
else TIMEDIFF(timestamp('2021:01:01',finish_time),timestamp('2021:01:01',start_time))
end as diff,
and it works
As 00:00:00 is considered the start of a new day or even if you have an end time that is the day after the start time you will have to make your start and end field into DATETIME types. Also if you want the difference between the 2 times you should be using timediff(end,start)
Related
I need to calculate the difference between two columns which are of type time. The fields are named start_time and end_time. I use the function timediff(s,e) and it works well but some times it doesn't. For example when end_time is 00:00 which is considered as 12 AM and start_time is 19:00 which is 7 PM the difference function shows 19 hours (actually I also select hour(timediff(s,e))) while I expect it to be 5 hours.
How can I fix this?
I tried this
case
when finish_time < start_time then TIMEDIFF(timestamp('2021:01:02',finish_time),timestamp('2021:01:01',start_time))
else TIMEDIFF(timestamp('2021:01:01',finish_time),timestamp('2021:01:01',start_time))
end as diff,
and it works
As 00:00:00 is considered the start of a new day or even if you have an end time that is the day after the start time you will have to make your start and end field into DATETIME types. Also if you want the difference between the 2 times you should be using timediff(end,start)
I have a table in Mysql which has column called 'dep_timestamp' which holds data in the following format (the data is received from a external source so can't be changed, and is displayed via web queries so can't be modified within that table)
2015-05-12 19:18:00 +0100
The database holds cancellations for booked taxi journeys which get pushed out to me from a central booking system in realtime. Throughout the day I will get any number of messages for cancelled journeys. A journey has a booked departure time dep_timestamp in its full format of 2015-05-12 19:18:00 +0100 that is used for reporting and all sort of other things.
Every day at 03:00 I want to delete all of the cancelled journeys that where due to depart 'yesterday' This means when my users do a query and ask what journeys have been cancelled today they only see stuff that has a booked departure of today.
I have an event setup on the server to delete rows older then 1 day using the following code;
DELETE FROM db.canx_today WHERE 'dep_timestamp' < DATE_SUB(CURRENT_TIME() , INTERVAL 1 DAY)
That event is set to run every day at 03:00 and does without error. However it takes the full date/time into consideration when running which means it only deletes the rows where the time & date are both older than one day.
If I swap CURRENT_TIME with CURRENT_DATE then the server throws this error; Truncated incorrect datetime value: '2015-05-13 10:17:00 +0100' which makes sense in so far that its looking for a full date/time string.
Is there a way to ignore the time element and just delete all rows that are from the previous day?
You can calculate based on CURRENT_DATE() and just concatenate 00:00:00 to that value.
WHERE `dep_timestamp` < CONCAT(CURRENT_DATE(), ' 00:00:00')
This should work, but will only be noticeably faster than the one I originally put in the comments above if dep_timestamp is indexed.
WHERE `dep_timestamp` < DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00")
Since DATE_FORMAT() actually returns a string, this might be more efficient when indexes are actually needed:
WHERE `dep_timestamp` < CAST(DATE_FORMAT(curdate(), "%Y-%m-%d 00:00:00") AS DATETIME)
DELETE FROM `canx_today`
WHERE DATE(`dep_timestamp`) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
I am trying to display data associated with date. However in my case, I don't want the date to start at 00:00:00 and finishes at 23:59:59. Rather I want it to start at 16:00:00 and finishes at 06:00:00 the next day. In other words I want to create a custom time for date.
In the same time I want to GROUP_BY date.
For instance I have these values in the database:
want it to give me:
date: 2013-09-08 count: 2
date: 2013-09-09 count: 1
I am not asking for code, but a way to think about it, or useful methods.
Thank you in advance!
The simplest method is to take the existing date and subtract six hours to get the "effective" date. You would do this for output purposes only.
Example:
select date(datecol - interval 6 hour) as MyDate, count(*)
from t
group by date(datecol - interval 6 hour);
You can use a where clause to remove the times between 6:00 and 16:00 (unless that is a typo).
I have a column in a table of type TIME. I want to get a result that applies a time shift that results in a 24 hour clock representation of that shift. To add the shift, my query contains...
select addtime(boo,'01:00:00') as yada
But any value that gets taken out of the 24 hour range ends up outside the 24 hour range, such as...
23:45 ends up as 24:45 (when I want 00:45:00)
If I go the other way and subtract the hour from a value less than 1am, I get...
00:15 ends up as -00:45:00 when I want (23:15:00)
Now, I understand that the TIME format in MYSQL is "duration" and not the actual "time", but for the life of me I can't figure out how to convert to an actual clock time as outlined above. Please help me or kill me. Either will end my suffering.
A simple solution would be to just use a DATETIME data type instead, then ignore the date part. If you're not dealing with huge amounts of data, or searching by the actual times I can't see an issue.
As a bonus you'll be able to manipulate the data with the likes of + INTERVAL 1 HOUR etc.
When extracting it just use TIME(boo)
As you know, the MySQL TIME type is not restricted to 24 hour time so this is probably the closest you'll get... I'm sure you could construct a query using MOD() etc but it's probably not worth it.
A possible solution is just add your boo TIME value to any date (e.g. today) then add your time delta and after that just return time part with TIME()
SELECT TIME(CURDATE()
+ INTERVAL TIME_TO_SEC('23:45:00') SECOND
+ INTERVAL 1 HOUR) new_time
Output:
+----------+
| new_time |
+----------+
| 00:45:00 |
+----------+
Here is SQLFiddle demo
I am working on a mysql query that will search a database of entries that each have a time span to see if a user input time or time span falls within the entries spans.
Ex user can input "13:00" or "13:00-16:00"
the database has entries like so:
id startTime endTime
1 08:00 12:00
2 20:00 03:00
3 14:00 01:00
4 16:00 21:00
Searching is easy enough against a time span that is during a single day (startTime < endTime). The issue is testing when a span goes across midnight (endTime < startTime). For this application these values can not have a date attachment (they are stored as time only), so timediff etc will not work.
UPDATE:
The time spans will never be greater than 24 hrs so startTime of 1 and endTime of 3 will always be a 2 hr item, not a 26 hr item.
The reason I am not using dates is this relates to something more or less like business hours (but not exactly that use case). The items the spans represent have daily hours, so it is not practical to add datetime stamps for every day.
I would check if endTime
totaltime = startTime + (24 hours - endTime)
The problem I see with this is if the endTime is the next day and endTime>startTime. For example you start at noon and end at 1pm the next day. There is also a problem if the time spans more than 2 days. For example start on day 1 and end day 3 or longer. I would recommend using dates.