Datediff on unknown hour interval - SQL Server 2012 - sql

I have a dataset which shows a datetimestamp for both the start and end of an agent status and I am trying to capture the duration within that hour interval of that status by using a simple datediff function in SQL Server 2012.
This works perfectly as long as the status start and end time are within the same hour. However, in cases where the status starts at 8:37 and finishes 10:15, I am having a hard time inserting an hour interval to perform the datediff function.
In the scenario above, I would like the data to show 23 minutes in the 8 am interval, 60 minutes in the 9 am interval, and 15 minutes in the 10 am interval.

Related

How Schedule a Azure Data Factory Trigger for 30 minute intervals per day

I how like a create a Azure Data Factory Triggger to run every day at 30min intervals. However, I don't seem to be able to create 30mins interval per day. The nearest I appear to get is 1 hour.
E.g, I would like 6:30, 7:00, 7:30, 8:00 etc.
But as you can see I appear to only schedule hourly per day
Why not this , its more simpler , its you want to start at :00 or:30 min mark .
Then set the start time as accordingly
I figure it out.
I simply adjusted the execution times to include start minutes of 0, with interval of 30

DateDiff to find duration in minutes not working

I am using DATEDIFF(MI,STARTDATE,ENDDATE)
I tried using MINUTE and N, but none of them are producing the expected result. Whenever the difference is 50 seconds, it is showing 1 minute.
I am using SQL Server 2008, and all the date times are in UTC format.
My Start Date is a database record and End Date is GETUTCDATE().
DATEDIFF returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate. For example, this query will return 1 as a minute boundary has been crossed between the two times:
SELECT DATEDIFF(mi,'5 May 2015 11:59:00','5 May 2015 12:00:01')
You could use this statement instead:
SELECT DATEDIFF(s,DATEADD(s,-50,GETUTCDATE()),GETUTCDATE()) / 60

sql Sum time that occurs in an interval

i have a lot of data that has at start time and a finnish time. These are formated i datetime format.
i want to sum the time that occurs in an timeinterval
if specify the time interval 08-11
i only want to get the time between these to even if the evvent progresses from 06 to 12
If you are using SQL Server you could do it like that:
SELECT SUM(DATEDIFF(HOUR,StartTimeColumn,EndTimeColumn)) AS ElapsedHoursTotal,
SUM(DATEDIFF(MINUTE,StartTimeColumn,EndTimeColumn)) AS ElapsedMinutesTotal,
SUM(DATEDIFF(SECOND,StartTimeColumn,EndTimeColumn)) AS ElapsedSecondsTotal,
FROM dbo.YourTable
You will have to find the perfekt interval (First Parameter of DATEDIFF Function) for your requirements... Hours, Minutes, Seconds, Nanoseconds,...
Im using mssql 2012
the problem is that i can get the full elapsed time from start to finnish but I only want the part that matches my search
if the pattern i match for is 8-11
thing one 08-12 should produce 3 hours
thing two 10-11 should produce 1 hour
thing tre 9.30- 14 shoud produce 1.5 hour

Server time is not aligning with local time

I have been using date_default_timezone_set('Asia/Manila'); in my website and it echoes out the correct time of my time and date but when I uploaded it to my server the curdate() and now() function gets the time in the server. How will I know how much time should I add or subtract to my sql so that it would align in the server time? I have already tested echoing the time in my website but it only shows the correct time and not the server time. Is there a way to know the time in the server?
EDIT
I am making a filter for my records in my database. Those are today's date, week, month, and past 3 months. I have successfully done that but the time in my localhost is not aligned with the server time that's why when I uploaded it into the server it only shows the records of today after 1pm in the afternoon. I tried adding DATE_SUB(CURDATE(), INTERVAL 13 HOUR) but it doesn't work. 13 hours because I remembered adding 13 hours so that the time showed in the table is aligned with the server time. But then when I added 24 hours it shows the records in the morning but in the afternoon it doesn't show any records at all. Then I tried adding 20 hours to 23 hours but no records was shown both in the morning and in the afternoon. How can I see what is the time of the server so that I can easily add or subtract hours interval so that the records will be shown in the corresponding filter?
This is my whole sql
SELECT *
FROM `report`
WHERE DATE(`Timestamp`) = DATE_ADD(CURDATE(), INTERVAL 24 HOUR)
AND ( Employee_ID IN (SELECT T1.Employee_ID
FROM `employee` T1
WHERE T1.Supervisor_ID = '".$id."'
)
OR Employee_ID IN (SELECT T2.Manager_ID
FROM `branches` T2
WHERE T2.Manager_ID = '".$id."'
)
OR Employee_ID = '".$id."'
)

How to subtract 1 minute from hour

I have a query contains from time and to time parameters.
I need to subtract 1 minute from totime parameter.
I used totime-1.
It is working in database level and it is not working in my crystal reports.I mean it is subtracting 1 hour in report level.
Can any one please tell me other formula for subtracting a minute.
Thanks,
vissu
If you are using SQL Serve/T-sql
DATEADD(MI, -1, totime)
In some databases (SQL Server and Oracle for instance), you can do the following:
select totime - 1.0/(24*60)
Time differences are measured in days, so 1.0/(24*60) is one minute represented as a fraction of a day.
MySQL accepts the syntax, but the timestamp is measured in seconds not days, so you would use:
select totime - 60