How to subtract time from date and time - mysql

I get a enddatetime field that is auto generated. I also have a totaltime that is being stored in to mysql. I want to subtract enddatetime from totaltime to get startdatetime and datetimeofevent.
Example
totaltime= 14:00:00
enddatetime = 5/14/2018 15:00:00
I am currently using date_sub function but its subtracting the hh from yy which is worng.
DATE_SUB(enddatetime , INTERVAL (TimeDIFF(totaltime, Time(enddatetime ))) HOUR)

You have to extract time part from enddatetime from the first part of date_sub

dateTimeDIFF function returns the difference in time format like 12:30:50
and
DATE_SUB expects the interval to be integer value
you need to convert from time format to integer
you can use time_to_sec function to convert the time to number of seconds then devide by 360 to convert seconds to hours or use seconds instead of hours.
example
DATE_SUB(enddatetime , INTERVAL (time_to_sec((TimeDIFF(totaltime, Time(enddatetime ))))/3600) HOUR)

Related

MYSQL Calculate date difference in digital time format

In one of the table I have 2 columns name date1 and date2 having datetime data type
I am calculating difference between these two dates using timediff(date2,date1). Now suppose
date1=2018-04-05 13:10:00
date2=2018-04-05 14:40:00
then the difference between these two dates will be 01:30:00
MY MAIN QUESTION IS how to convert this H:i:s time to digital time format like 01:30:00=1.5 or 01:45:00=1.75?
Use time_to_sec to convert to seconds. Then divide by 3600 (60 seconds per minute; 60 minutes per hour) to get to hours:
select time_to_sec(timediff(timestamp '2018-04-05 14:40:00',
timestamp '2018-04-05 13:10:00')) / 60 / 60;
By the way, you can also use timestampdiff instead of timediff to get seconds right away:
select timestampdiff(second, timestamp '2018-04-05 13:10:00',
timestamp '2018-04-05 14:40:00') / 3600;
select hour(timediff(date1, date2)) + minute(timediff(date1, date2))/60 + second(timediff(date1, date2))/60

How sum 24 hours to time mysql?

I've got a column time type in mysql, i want to add 24 hours to this hour, i try with code below:
SELECT SUBSTRING(CAST(DATE_ADD(STR_TO_DATE('23:00', '%k:%i'), INTERVAL (TIME_TO_SEC('24:00') / 60) MINUTE) AS CHAR(8)), 1,5)
I need to return 23:00 again (because i add 24 hours but that 23:00 is of the next day) but this code return me 47:00.
Some help?
I think you're looking for ADDTIME
SELECT ADDTIME(‘23:00’,’24:00’)
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_addtime
You need to first convert the column value to a datetime, add the hours, and then convert the result back to time:
select cast(date_add(cast(time_column as datetime), interval 24 hour ) as time)
from yourtable;
Example:
select cast(date_add(cast(cast('23:00' as time) as datetime), interval 24 hour ) as time)
The TIME type allows you to store up to (but not including) 839 hours (positive and negative). That's great if you need to store duration, but not so much if you want to store time of day. If you want the latter you should consider the DATETIME type instead.

Hive Query Language Timestamp

I need to get the timestamp of interval of 7 days from the current time in milliseconds. I tried date_sub using now() but didn't work for me. How do we do this in hive. I need exactly the interval current_timestamp(unix) and interval of 7 days from the current in my query. Also is there any provision to select the time zone like UTC + 5:30 hrs like that?
I could not find information about millisecond based time calculations in HIVE.
unix_timestamp() is the current timestamp, but it does not have milliseconds.
The offset is 7 days*24 hours/day*3600 secs/hour = 604800 milliseconds
So the timestamp of the current time plus 7 days would be unix_timestamp() + 604800
The UTC part is trickier; you can use to_utc_timestamp, giving it your calculated timestamp, and the timezone it is coming from (as a date). It will return a date string, which you will pass through unix_timestamp()
In other words, assuming it is coming from PST, you should use:
select unix_timestamp(to_utc_timestamp(from_unixtime(unix_timestamp() + 604800), 'PST')) from dual;
See the documentation here:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

Mysql Time Arithmetic - Time only

This should be so easy... but is driving me mad.
UPDATE time SET time = (time - interval 130 minute) WHERE stuff=whatever;
Time is a time column only, i.e. 09:00:00.
Assuming that you would like to subtract 130 minutes from the current time, you can use addtime, like this:
UPDATE time SET time = addtime(time, '-02:10') where stuff=whatever
130 minutes is 2 hours and 10 minutes, hence the -02:10 constant.
Here is a quick demo on sqlfiddle.
Change - to , and it will work. The correct Query is:
UPDATE time SET time = (time, interval 130 minute) where stuff=whatever
If time is a datetime or a timestmap, you must use a date_sub funktion
SELECT date_sub(time, interval 130 minute) FROM ....
Otherwise you can also convert your time with UNIX_TIMESTAMP, sub it and convert with FROM_TIMESTAMP into a mysql timestamp back
There is a DATE_SUB method that works like the DATE_ADD method you are looking for.
DATE_SUB(NOW(),INTERVAL 130 MINUTE)
Check this link for more information:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub

How to Subtract Days in MySQL

How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
Not entirely related to this question but is related to the title:
SELECT SUBTIME("10:24:21", "5"); -- subtracts 5 seconds. (returns "10:24:16")
SELECT SUBTIME("10:24:21", "01:00:00"); -- subtracts one hour. (returns "09:24:21")
Documentation: MySQL SUBTIME function
Use:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
You can use this :
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
In order to subtract the time instead, say 15 minutes, the following should work:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
Yes its possible using date function in Mysql
select distinct
lastname,
changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I have subtract 15 days from my date - check image please. thanks