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
Related
how to get round of last hour between last hour and exactly 1 hour before , for example if now the hour is 14:10 the anser i am looking fir is give me all record between 13-14:00, my code is only for last hour :
select *
from my_table p
where p.when_seen between unix_timestamp(now())- 3600 and unix_timestamp(now())
wheen_seen is linux timestamp for example
select when_seen ,from_unixtime(when_seen ) from my_table LIMIT 1;
1539085264 2018-10-09 11:41:04
Divide the Unix timestamp by the number of seconds an hour has (3600), floor that division and multiply it by the number of seconds an hour has. Then oyu have the timestamp of the hour without minutes or seconds.
SELECT *
FROM my_table p
WHERE p.when_seen >= floor(unix_timestamp(now()) / 3600) * 3600 - 3600
AND p.when_seen < floor(unix_timestamp(now()) / 3600) * 3600;
You should also considering using a right open interval as the right boundary isn't part of the hour before.
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)
I'm trying to subtract now() - the created datetime field from 30 days to get the days remaining as a datetime field, mysql gives me an error for this sort of thing.
SELECT id, created, INTERVAL 30 DAY - CURRENT_DATE - created as timeleft FROM tablename
Use the Date add function to subtract 30 days like this.
SELECT DATE_ADD(current_date, interval -30 day);
Return the difference in days between two date values:
SELECT DATEDIFF(current_date, "2017-06-15");
You can use the combination of these functions to achieve the desired result.
In mysql you should translate the datetime to unix_timestamp to calculate the difference between the two days
SELECT id, created, 30 - (unix_timestamp(CURRENT_DATE) - unix_timestamp(created )) / 3600/24 as timeleft FROM tablename
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.
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