MYSQL Seconds column for time - mysql

I have a seconds column and I need to display in some readable format.
Table A
Time
259200
select Time from Table A
and I need to get :
3 days,00:00:00 I need to have no of days if > 24 hrs.
I tried lots of way but I was not able to get the right answer.
A few things which I tried are:
select floor(hour( sec_to_time
Select sec_to_time
SELECT TIME_FORMAT( MAKETIME(

How about this:
SELECT
CONCAT_WS(' ',
`time` DIV 86400, 'day(s) and',
SEC_TO_TIME(MOD(`time`, 86400)), 'hours'
)
FROM TableName;
time DIV 86400 gives you the number of day(s), SEC_TO_TIME(MOD(``time``, 86400)) gives you the h:m:s remaining. Then just concat them.
Output:
0 day(s) and 01:50:00 hours
1 day(s) and 10:17:36 hours
101 day(s) and 10:50:32 hours
plz see sql fiddle.

Check the MySQL manual for time and date functions. For example, SEC_TO_TIME and TO_DAYS.
UPDATE:
Quite right, I misread. The following is a terrible hack but should give you the answer you want. For a real system, I would write a proper function for this.
concat(floor(hour(sec_to_time(x))/24), " days, ", subtime(sec_to_time(x),concat(floor(hour(sec_to_time(x))/24)*24,":00:00")))

Related

MySQL Trigger - timestamp hour between?

I'm after a mysql trigger that will set the column peak to 0 or 1 based on the timestamp hour between 08:01 & 23:59.
Can anyone help?
example:-
userstats.timestamp = 2018-07-01 12:27:20
peak = 1
One option is to use the DATE_FORMAT function to isolate the time component of your timestamp, e.g.
SELECT *
FROM userstats
WHERE DATE_FORMAT(timestamp, '%H:%i') BETWEEN '08:01' AND '23:59';
Your actual query may not look exactly like this, but this would seem to answer the crux of your question.

Mysql: Round datetime to seconds - some issues 24h

i spend time to solve my issue on my own and also read through many posts in here but somehow i cannot find a working solution for me.
I'm collecting different values from my smarthome in an mysqldb. To run some analysis on the data i need to round a datetime value in one usecase. I must get rid of the seconds.
What I tried/found where different approaches (even via Unixtime) but nothing solved my issue. Within this forum i found an old thread with the following hint:
extract from my insert script:
CONVERT(
CONCAT(
date(`detailed-data-integration`.`timestamp`),
' ',
SEC_TO_TIME((ROUND(TIME_TO_SEC(`detailed-data-integration`.`timestamp`)/60)) * 60)
) , Datetime) AS `timestamp-rnd`
This looks fine and works on the first view, but:
Timestamp: 2018-02-03 23:59:56 leads to a NULL because of an invalid datetime value. The function wants to create: 2018-02-03 24:00:00 which is wrong.
Can somebody of you give me a hint how to handle / solve this issue?
Thx and KR
i identified a solution which will work for my requirements which i want to share with you:
CASE
when second(`detailed-data`.`timestamp`) >= 30 then DATE_FORMAT(`detailed-data`.`timestamp`, '%Y-%m-%d %H:%i:00') + interval 1 minute
when second(`detailed-data`.`timestamp`) < 30 then DATE_FORMAT(`detailed-data`.`timestamp`, '%Y-%m-%d %H:%i:00')
END as testcase
Why are you using time_to_sec()? You should be using to_seconds()
'0000-01-01' + interval round(to_seconds(`detailed-data-integration`.`timestamp`)/60)) * 60) second - interval 1 day as `timestamp-rnd`
In other words, don't separate the date from the time. Just handle the entire date/time as a single value.

The difference between datetime data in SQL

how can i find the difference between two dateTime store in a MySQL database
e.g the difference in hours between 2016-03-09 04:30:00 and 2016-03-10 03:00:00.
i have tried dateDiff() but it does not compare the hours that is need to see the difference between (2016-03-09 04:30:00) - (2016-03-10 03:10:00).
the order is year-month-day time
The output i need is the number of hours between these times also considering the time as well.
You can use TIMESTAMPDIFF to find the difference between two timestamps
SELECT TIMESTAMPDIFF(HOUR,'2009-05-18 10:00','2009-05-18 11:00');
If you want fraction(eg: 1.5 hrs) hours you can do like below
SELECT (UNIX_TIMESTAMP('2012-10-30 10:40')-UNIX_TIMESTAMP('2012-10-30 10:30'))/3600 hour_diff
One simple method is to use to_seconds():
select to_seconds(dt1) - to_seconds(dt2)
This gives the difference in seconds. Then you can divide by 60*60 to get hours or 24*60*60 for days.

DateDiff function in MYSQL 5.1. Need Hours+Mins

MYSQL 5.1
I need to create a query that gives my result table three columns, match_date, match_start and match_duration
I need the match_duration column which takes match_start and match_end and displays how long each game has been on for in 00:00 hours format. So for example if it were 5hrs:30 mins it would be displayed 5:30 hours.
This is what I have so far and not sure where I'm going wrong:
SELECT match_date, match_start, DateDiff(hhmm, match_start, match_end) AS Duration
FROM MatchInfo;
Thanks
Try below:
TIME_FORMAT(TIMEDIFF(match_end, match_start), '%H:%i')
i.e.
SELECT match_date, match_start,
TIME_FORMAT(TIMEDIFF(match_end, match_start), '%H:%i') AS DURATION
FROM MatchInfo;

What instead of CONVERT(TIME, x) Datepart?

I have complicated query over very big table.
Long story short, when I use convert time to select period of day (let's say 12-13h, converting it from datetime row) query takes few minutes, instead of few seconds without convert!
So, I tried datepart, and it works well, almost instant, but, problem is, how to point to hours and minutes in same time?
Any other fast solution is more than welcome.
Thanks.
Meanwhile I came up with this:
DATEPART(HOUR, datetimecolumn)*100 + DATEPART(MINUTE, datetimecolumn)) between 1210 and 1540
You can use datePart if you are willing to do a bit of math, as shown below:
12:10 = 12 * 60 + 10 = 730 minutes
15:40 = 15 * 60 + 40 = 940 minutes
select * .....
where datepart(mi, datefield) between (12*60+10) and (15*60+40)
If you have a constant periods - i.e. - always hourly and no any floating periods - you may introduce something like "ordinal number of period" calculated field, index on it and query of it with precalculated period value
OR
is there are no any constant periods - try to calculate proper begin and end values prior to SELECT statement and use them in the query.
Keep in mind that using functions in where clause of query - sometimes is a bad idea. Using functions in ORDER BY clause - always bad
You can get GETTIME from following Function
alter function GetTimeOnly(#_DateTime DateTime)
returns datetime
as
begin
return dateadd(day, -datediff(day, 0, #_datetime), #_datetime)
end
go
OR YOU CAN HAVE THE TIME FROM CONVERT FUNCTION.
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly