Convert float day of year in datetime mysql - 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

Related

Mysql DATE_ADD 1 hour and I want minutes be 15, or 30

What I want to do is Add 1 hour to current date + make the minutes be in 15 minutes or 30 minutes.
Example:
'2022-05-19 22:13:28'
I want it to be :
'2022-05-19 23:15:00'
Another example
'2022-05-19 22:24:00'
I want it to be:
'2022-05-19 23:30:00'
SELECT FROM_UNIXTIME(3600 + 900 * CEIL(UNIX_TIMESTAMP(datetime_column) / 900))
FROM table
UNIX_TIMESTAMP converts datetime to the seconds amount.
/ 900 calculates the amount of 15min. blocks in it.
CEIL convert incomplete last block to complete one.
* 900 converts blocks amount back to seconds.
+ 3600 adds an hour.
FROM_UNIXTIME converts seconds amount to datetime.
Assuming if it already on an even 15 minutes it just gets an hour added, you have to do some math on the time, then re-add it to the date:
select date_add(date(d), interval ceil(time_to_sec(d)/900)*900+3600 second)
from (select '2022-05-19 22:13:28' d union all select '2022-05-19 22:24:00' union all select '2022-05-19 22:15:00' union all select '2022-05-19 21:59:00' union all select '2022-05-19 21:44:00') foo
Dividing by 900 seconds, ceil'ing, and multiplying by 900 rounds it up to the nearest even 15 minutes.

Rounding time in Python in SQL

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.

How to get half hour if exist with mysql hour function

I want to get 8:30 hour instead of 8 from below query
HOUR(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')
Mysql hour function only return number of hour not half hour
We can get the difference between two datetime values in SECOND units, using TimeStampDiff() function. Now, we can convert this into Time using Sec_To_time() function.
SELECT SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2018-12-01 07:00:00','2018-12-01 15:30:00'))
Result
| SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2018-12-01 07:00:00','2018-12-01 15:30:00')) |
| ------------------------------------------------------------------------------- |
| 08:30:00 |
You may call both HOUR and MINUTE, the latter to get the minute component:
SELECT
HOUR(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')) +
MINUTE(TIMEDIFF('2018-12-01 07:00:00','2018-12-01 15:30:00')) / 60.0 AS hours
FROM yourTable;
This outputs 8.5 for the number of hours.
Another option would be to first convert both timestamps to UNIX timestamps in seconds since epoch. Then convert their difference back to hours:
SELECT
(UNIX_TIMESTAMP('2018-12-01 15:30:00') -
UNIX_TIMESTAMP('2018-12-01 07:00:00')) / 3600.0 AS hours
FROM yourTable;
Demo

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

MySQL: How to convert seconds to mm:ss format?

I want to convert seconds to minute : seconds format in sql select statement.
At the moment I am using:
SELECT SEC_TO_TIME(duration) from messages;
It works perfectly but it gives me this format of time: hh:mm:ss
but I need mm:ss
Is it possible to convert seconds into mm:ss format using sql query?
If the value is less than an hour, then just do:
SELECT RIGHT(SEC_TO_TIME(duration), 5) from messages;
If you might go over an hour, then do the arithmetic:
SELECT CONCAT_WS(':', FLOOR(SEC_TO_TIME(duration) / 60),
SEC_TO_TIME(duration) % 60)
I recently had a similar project where I needed to convert stored seconds to m:ss format. No matter the amount, there needed to be at least one digit representing minutes and two digits representing seconds. The hours placeholder was forbidden, so the minutes value could acceptably go beyond 59 and also contain more than 2 digits. The minute value must not be zero-padded.
This is what I used: (SQLFiddle Demo)
CONCAT(FLOOR(seconds/60), ':', LPAD(MOD(seconds,60), 2, 0)) AS `m:ss`
aka
CONCAT(FLOOR(seconds/60), ':', LPAD(seconds%60, 2, 0)) AS `m:ss`
seconds | m:ss
-----------------
0 | 0:00
1 | 0:01
10 | 0:10
60 | 1:00
61 | 1:01
71 | 1:11
3599 | 59:59
3600 | 60:00
5999 | 99:59
6000 | 100:00
TIME_FORMAT(SEC_TO_TIME(seconds),'%i:%s') was unsuitable because the project specifications did not want the minute portion to be zero-padded. Here is a good post relating to this technique.
There is no single-digit minute option in TIME_FORMAT() or DATE_FORMAT().
If you are using MySQL 8.0+ you can use REGEXP_REPLACE like this to achieve a variable length string similar mickmackusa's answer:
REGEXP_REPLACE(SEC_TO_TIME(duration), '^(0|:)+', '')