Rounding time in Python in SQL - mysql

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.

Related

Find How many minutes a month has: MySQL

Say I have a column
Date
23-03-2019
04-04-2019
I want to find hoe many minutes the whole month has in MySQL.
Expected output:
Date MinsinMonth
23-03-2019 44640
04-04-2019 43200
Basically, you just want to find the number of days in the month and then do some multiplication. For this, use last_day():
select day(last_day(date)) * 24 * 60 as minutes_in_month
This should work:
SELECT DAY(LAST_DAY(Date)) * 1440 AS MinsinMonth
LAST_DAY returns the last day in the month a date is in
DAY Returns the day number associated to a date
1440 is the number of minutes per day (60 * 24)

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

Convert float day of year in datetime mysql

I have the following table in MySQL DB:
year | day_of_year
2012 | 283.813090
How to convert these fields in DateTime?
I tried:
SELECT MAKEDATE(year, day_of_year) from tableName;
It returns the Date (yyyy-mm-dd)... How can I get the Time (hh:mm:ss)?
Can anyone help me solve this problem?
By using MySQL's SEC_TO_TIME() function (and a little math), you'll be able to get your timestamp.
The math involved would be to multiply your day_of_year column by the number of seconds in a day, 86400, and then mod-the result on 86400, giving you the equation ((day_of_year * 86400) % 86400):
SELECT SEC_TO_TIME((day_of_year * 86400) % 86400) AS time;
If I run this with the value 283.813090, it gives me the correct time-of-day*:
mysql> SELECT SEC_TO_TIME((283.813090 * 86400) % 86400) AS time;
+----------+
| time |
+----------+
| 19:30:51 |
+----------+
To combine this with the actual date, you can either select them in two separate columns or use CONCAT() to get a real "timestamp":
SELECT
CONCAT(MAKEDATE(year, day_of_year), ' ', SEC_TO_TIME((day_of_year * 86400) % 86400)) AS timestamp
FROM tableName;
* The math behind the time-of-day calculation is fairly straightforward. For 283.813090, this says that there are 283 days. To calculate the time-of-day, we use the fractional portion and multiply by 24 (for the hours). .813090 * 24 = 19.51416. This says there are 19 hours; to calculate minutes, we take the fractional portion of this and multiply by 60. .51416 * 60 = 30.8496, which says 30 minutes. For the seconds, again, take the fractional part and multiply by 60: .8496 * 60 = 50.976. So, altogether we have 19 hours, 30 minutes and 50 seconds (with .976 milliseconds, which rounds up) - 19:30:51, or 7:30pm.
Here's the query I came up with:
SELECT TIMESTAMP(
MAKEDATE(year, day_of_year),
SEC_TO_TIME((1 - (round(day_of_year) - day_of_year)) * 24 * 60 * 60))
FROM tableName;
What's this?
1 - (round(day_of_year) - day_of_year) => getting the fractional part ( .813090 )
24 * 60 * 60 => total seconds in 1 day = 24 hours * 60 minutes * 60 seconds
Testing
You can play with the query using the SQL Fiddle here: http://sqlfiddle.com/#!2/f62619/21
Results
These are the results with the two samples you provided in the question:
October, 10 2012 19:30:51+0000
October, 10 2012 20:15:23+0000
References
These are nice resources that helped me construct the query:
12.7. Date and Time Functions
How to add date or time values in MySQL

MySQL Query to get Time Before and After specified Time

I am looking to pull data from my database for a time interval of 90 Minutes (before and after) a specified date and time. Currently I am using below query but it's returning zero records. Please help me in doing this.
SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date`
BETWEEN from_unixtime( 1324363500 ) - INTERVAL 120
MINUTE AND from_unixtime( 1324363500 ) + INTERVAL 120
MINUTE
LIMIT 0 , 30
Thanks in advance
You need to use DATE_ADD and DATE_SUB
SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date` BETWEEN
DATE_SUB(from_unixtime(1324363500), INTERVAL 90 MINUTE)
AND
DATE_ADD(from_unixtime(1324363500), INTERVAL 90 MINUTE)
LIMIT 0 , 30
SQLFiddle Demo
SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date`
BETWEEN 1324363500 - 60*90 AND 1324363500 + 60*90
LIMIT 0 , 30
90 - is number of minutes, you can change this value.

getting number of hours until the next event

I've got a table with this data:
[ID] [event_name] [last_event]
1 stats 2011-01-01 01:47:32
last_event is a timestamp. The event occurs every 48 hours (it's a cron job). I'd like to show my users the number of hours until the event executes again.
So far I've got:
SELECT (lastFinish + INTERVAL 48 HOUR) FROM `cron_status`
which gives me the exact time and date of the next occurence: 2011-01-03 01:47:32. So I figured if I subtracted the current datetime...
SELECT ((lastFinish + INTERVAL 48 HOUR) - SYSDATE()) FROM `cron_status`
which (I think?) gives me the difference in unix time: 1980015. But if I divide that by 3600 to convert the seconds to hours...
SELECT (((lastFinish + INTERVAL 48 HOUR) - SYSDATE())/3600) FROM `cron_status`
I get numbers an order of magnitude too high: 549.99.
Where am I going wrong? The target is returning the number of hours until the next execution.
Thank you!
The result can be obtained directly, using the timediff() MySQL function:
SELECT timediff(lastFinish + INTERVAL 48 HOUR, now()) FROM cron_status;
should display the time as hh:mm:ss. Assuming lastFinish is a datetime.
In order to get the answer in hours instead,
SELECT timestampdiff(HOUR, now(), lastFinish + INTERVAL 48 HOUR) FROM cron_status;
Note that timediff does arg1 - arg2 while timestampdiff does arg2 - arg1.