Mysql how to get exact day and time between two dates - mysql

I am passing date and time as input.
'2015-01-12 10:30:00' and '2015-01-13 11:30:00' like this.
we can find the days, hours, minuets, seconds etc between the timestamps. by using the FUNCTION called timestampdiff().
I tried this query.
SELECT TIMESTAMPDIFF(HOUR,'2015-01-12 10:30:00','2015-01-13 11:30:00')/24 FROM DUAL
but it gives me output like 1.04.
My requirement is exact day and hours between that two dates.
Ex:
'2015-01-12 10:30:00' and '2015-01-13 11:30:00' for this
OUTPUT :
1 day and 1 hrs.
thank you.

Just call that function twice. To have the result with the sentence you want, you can do something like this:
SELECT CONCAT(
TIMESTAMPDIFF(DAY,'2015-01-12 10:30:00', '2015-01-13 11:30:00'),
" day(s) and ",
TIMESTAMPDIFF(HOUR,'2015-01-12 10:30:00', '2015-01-13 11:30:00') % 24,
" hour(s)");
If you also want month or minute interval, just add one more function call and the appropriate modulo number

This query can give you required output
SELECT CONCAT(
TIMESTAMPDIFF(DAY,'2015-01-12 10:30:00', '2015-01-13 11:30:00'),
"day(s) and ",
TIMESTAMPDIFF(HOUR,'2015-01-12 10:30:00', '2015-01-13 11:30:00') % 24,
" hour(s)");

Related

datediff....does not make sense

Below is my data, I'm trying to get the deference between 2 dates (Punch in and schedule in) to create a report for employees who comes early to work.
I used the following code in expression but the results is way off! Can any one help?
Could it be the formatting of date?
=DateDiff("h", Fields!Scheduled_In.Value, Fields!Punched_In.Value)
& ":" & DateDiff("n", Fields!Scheduled_In.Value, Fields!Punched_In.Value) mod 60
I'm not sure how or why you are getting those results, unless you datetime is actually a string that is being converted.
Anyway, I reproduced your data using a standard datetime format, here's the dataset query with the sample data in.
DECLARE #t table (ANumber int identity(1,1), Scheduled_In datetime, Punched_In datetime)
INSERT INTO #t (Scheduled_In, Punched_In) VALUES
('2022-05-30 09:45:00', '2022-05-30 10:04:48'),
('2022-05-30 12:00:00', '2022-05-30 11:46:09'),
('2022-05-30 14:00:00', '2022-05-30 13:56:44'),
('2022-05-30 14:00:00', '2022-05-30 14:01:30'),
('2022-05-30 09:45:00', '2022-05-30 09:21:11'),
('2022-05-30 09:45:00', '2022-05-30 09:57:18'),
('2022-05-30 14:00:00', '2022-05-30 13:55:37')
SELECT * FROM #t
I then used your original expression which actually works as I expected although the results are probably not what you want.
I then added a new column and used the following expression.
=DATEADD(
DateInterval.Second,
ABS(DateDiff(DateInterval.Second , Fields!Scheduled_In.Value, Fields!Punched_In.Value)),
DateSerial(2000, 01, 01)
)
What this does workout the absolute difference in seconds between the two fields.
It then adds this number of seconds to a fixed date (2020-01-01 although the date used does not matter).
So if the difference was say 1 hour and 5 minutes and 15 seconds the result would give us 2020-01-01 01:05:15
Then I changed the format expression of the textbox to this..
=IIF(
DATEDIFF(DateInterval.Second, Fields!Scheduled_In.Value, Fields!Punched_In.Value) > 0
, " - "
, " + "
) & "HH:mm:ss"
All this does is start with a + or - depending on if the scheduled time is earlier or later then the punched time, followed by a standard time format string. Basically it hides the date portion and puts +/- in front of the time.
Note This will not give the correct results if there is more than 24 hours between the times.
here's the result

SQL where two time values between two time columns

With MySQL, I need to find rows where two values are between two columns.
For example, I'm creating a schedule from '15:00' to '18:00', but the query must verify if any row has an period in use. If there is a record from '14:00' to '18:30', I need to return it to validate.
I tried something like, but not works:
select * from availabilities where (('15:00' or '18:00') between start_hour and end_hour)
Any idea?
This is basically a "test if range overlaps another range" question. The following query should work as long as start time is less than end time:
SELECT *
FROM availabilities
WHERE '18:00' > start_hour AND end_hour > '15:00'
You can use TIME() function to extract the time part of the datatime/timestamp column
Eg.
SELECT * FROM Table
WHERE TIME('timeStampCol') BETWEEN '12:00:00' AND '18:00:00'

Convert integer into datetime (dd:hh:mm:ss)

SELECT AVG (closeTime - createTime)
FROM Deals
WHERE dealid = 123
The 'closeTime' and 'addTime' have a DATETIME ( YYYY-MM-DD HH:MI:SS ) format. The result is:
105030215.0000
Which function should I use to convert this value into a DATETIME?
addTime: 12/04/2016 13:06
closeTime: 12/05/2017 16:08
Result that I am looking for (which I calculated in Excel):
29:03:02:15 (DD:HH:MM:SS)
Subtracting DATETIMEs in MySQL does not appear to give the difference between them in seconds. With your two example dates, 2016-04-12 13:06 and 2016-05-12 16:08 (assuming the 2017 in your question is a typo), subtracting them returns 100030200 whereas the correct answer is 2602920. This is out by a factor of 38.4 and a bit.
The MySQL function to return the difference in seconds is TIMESTAMPDIFF and, indeed, SELECT TIMESTAMPDIFF(SECOND, '2016-04-12 13:06', '2016-05-12 16:08') returns the correct number of seconds.
Now all we need to do is convert it into your format. MySQL has a built-in function SEC_TO_TIME which sort of does this, but unfortunately it doesn't work for periods of more than 839 hours and it doesn't itemise the days separately. We can work around this by calculating the days separately from the rest of the calculation and using SEC_TO_TIME on the leftovers once the days have been subtracted from the difference.
SELECT CONCAT(
TIMESTAMPDIFF(DAY, '2016-04-12 13:06', '2016-05-12 16:08'), ':',
SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2016-04-12 13:06', '2016-05-12 16:08') % 86400));
-- 30:03:02:00 (DD:HH:MM:SS)
This appears to be the correct answer. Although your desired output was 29:03:02:15, there were 30 days in April, and no seconds in the inputs at all.
As you're using an aggregate function to determine the number of seconds, we won't be able to select the value in days separately from the value in seconds (otherwise we'd get the average number of days followed by part of the average number of seconds), so you'll have to rewrite this as
SELECT CONCAT(
FLOOR(AVG(TIMESTAMPDIFF(SECOND, createTime, closeTime)) / 86400), ':',
SEC_TO_TIME(AVG(TIMESTAMPDIFF(SECOND, createTime, closeTime)) % 86400))
FROM deals WHERE dealid = 123;
-- 30:03:02:00.0000
This now includes fractions of a second because the average difference might not be a round number of seconds. To exclude them, use FLOOR or ROUND.
SELECT CONCAT(
FLOOR(AVG(TIMESTAMPDIFF(SECOND, createTime, closeTime)) / 86400), ':',
SEC_TO_TIME(ROUND(AVG(TIMESTAMPDIFF(SECOND, createTime, closeTime))) % 86400))
FROM deals WHERE dealid = 123;

Splitting a Time into Twelve Equal Parts

I am trying to get value of 12 equal parts of the night length.
This is what my table looks like:
sunrise_time sunset_time Day_Length Night_length
2014-01-01 06:02:41.000 2014-01-01 20:44:05.000 14:41:24.0000000 09:18:36.0000000
This is my query, but getting day_length instead of night_light:
select (convert(varchar(10),dateadd(ss,abs(datediff(ss,sunrise_time,sunset_time))/12,0),8)) as nighthour
from table1
Expected output: 00:46:33
Actual output: 01:13:27
What's wrong with my query?
Switch your start date and end date around in the datediff to avoid issues. Here's a small change to your code.
select convert(varchar(10), dateadd(ss, datediff(ss, 0, night_length) / 12, 0), 8)
from table1

MYSQL Seconds column for time

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")))