DateDiff to find duration in minutes not working - sql

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

Related

how to write a sql to calculate working hours minus rest time

I have a table of rest time in work shift
Begin end
12:00 12:30
17:30 18:30
Now I want to write a SQL to calculate actual working hours given the start and end time. For example if start at 9:00 and end at 15:00, the actual hours is 6-rest time=5.5 hours and if start at 9:00 and end at 20:00 the actual hours is 10 hours. How to write a procedure to check it in SQL server? Thx.
There are no schema details to work with here, which means the following SQL is generic and will have to be altered to fit your db.
SELECT
(datediff(minute, shiftStartTime, shiftEndTime)
- datediff(minute,breakStartTime,breakEndTime)) / 60.0
FROM yourTable
Notes:
If they can have multiple breaks, you need to sum up all the break times in minutes before deducting it from the shift period.
the calculation is specifically in minutes because the datediff counts the number of boundaries passed, so the date diff in hours between 11:59 and 12:01 is 1, even though the break is 2 minutes, you would count that as 1 hour if you count hours using the function.
If you can provide more schema details, we would be able to craft a more complete statement.
you can try below way using DATEDIFF
select *, CONVERT(time(7),DATEADD(s, DATEDIFF(s,S,E),'00:00:00')) from QQ
http://sqlfiddle.com/#!18/01213d/1
for your case column name will be
select *, CONVERT(time(7),DATEADD(s, DATEDIFF(s,Begin,end),'00:00:00')) from yourtable

Select records from after last 6 AM

Have table: item (int) and timestamp (datetime).
Need to know if there are any records with timestamp after last 6 AM.
Example:
At 5 AM it should check if there are any records from after 6 AM
yesterday. AT 7 AM it should check if there are any records from
after 6 AM today
This could be done of course by making a variable with datepart as:
if time now is < 6 AM datepart should be yesterday if time now
is >= 6 AM datepart should be today
but there must be a simpler way ?
This expression should always return the most recent 06:00 in the past:
select DATEADD(hour,
(DATEDIFF(hour,'2014-01-01T06:00:00',CURRENT_TIMESTAMP)/24)*24,
'2014-01-01T06:00:00')
It works by asking how many hours have happened since some arbitrary, known, 6AM. We then round this number down to the closest multiple of 24 (by dividing and multiplying with integer maths), and add this number back onto the same, arbitrary, 6AM

DATEDIFF - How many days until Sunday

In SQL Server, trying to write a age-off report for inventory purposes. Each week, the inventory system marks thousands of rows for deletion. This takes place on Sundays # 06:00:00 as part of weekly SQL DB purge schedule.
Using (yyyy-mm-dd hh:mm:ss:ms) format for closed_time, how can I calculate the numbers of days between that date, until next Sunday of the current week? And to be more elaborate, is there a way to narrow it down to the exact DD:HH:MM? The problem is the each client's Sunday DB schedule for purge varies. So that might be difficult to compute. Might be easier to just calculate whole days until Sunday 00:00:00. I tried using the DATEDIFF function with no success.
SELECT
Yada
DATEDIFF(DAY, closed_time,DW) AS Days_Until_Purged
FROM DB1
WHERE closed_time DESC
Thx in advance
If you choose any Sunday in the past (Such as 06:00 Sunday 2nd January 2000), you can calculate time that has GONE BY since then.
Then, if you take that and do modulo 7-days you get the time that has gone by since the most recent Sunday.
Then, if you do 7 - time_gone_by_since_last_sunday you get the time until the next sunday.
I'm going to do this in minutes to cope with a client that has a setting of 06:30.
DECLARE
#batch_processing_time SMALLDATETIME
SET
#batch_processing_time = '2000-01-02 06:00'
SELECT
(60*24*7) - DATEDIFF(minute, #batch_processing_time, closed_time) % (60*24*7)
FROM
yourTable
That's the number of minutes from each record's closed_time until the next #batch_processing_time.
Divide by (24*60) to get it in days.
try this:
select 8-DATEpart(w, closed_time) AS Days_Until_Purged from DB1 ...
This should solve your problem
SET DATEFIRST 1
Select DATEDIFF(dd,GETDATE(),DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),GETDATE()))

datetime manipulation: replace all dates with 00:00 time with 24:00 the previous day

I have a table described here: http://sqlfiddle.com/#!3/f8852/3
The date_time field for when the time is 00:00 is wrong. For example:
5/24/2013 00:00
This should really be:
5/23/2013 24:00
So hour 00:00 corresponds to the last hour of the previous day (I didn't create this table but have to work with it). Is there way quick way when I do a select I can replace all dates with 00:00 as the time with 24:00 the previous day? I can do it easily in python in a for loop but not quite sure how to structure it in sql. Appreciate the help.
All datetimes are instants in time, not spans of a finite length, and they can exist in only one day. The instant that represents Midnight is by definition, in the next day, the day in which it is the start of the day, i.e., a day is closed on its beginning and open at its end, or, to phrase it again, valid allowable time values within a single calendar date vary from 00:00:00.00000, to 23:59:59.9999.
This would be analogous to asking that the minute value within an hour be allowed to vary from 1 to 60, instead of from 0 to 59, and that the value of 60 was the last minute of the previous hour.
What you are talking about is only a display issue. Even if you could enter a date as 1 Jan 2013 24:00, (24:00:00 is not a legal time of day) it would be entered as a datetime at the start of the date 2 Jan, not at the end of 1 Jan.
One thing that illustrates this, is to notice that, because of rounding (SQL can only resolve datetimes to within about 300 milleseconds), if you create a datetime that is only a few milleseconds before midnight, it will round up to midnight and move to the next day, as can be seen by running the following in enterprise manager...
Select cast ('1 Jan 2013 23:59:59.999' as datetime)
SQL server stoers all datetimes as two integers, one that represents the number days since 1 Jan 1900, and the other the number of ticks (1 tick is 1/300th of a second, about 3.33 ms), since midnight. If it has been zero time interval since Midnight, it is stll the same day, not the previous day.
If you have been inserting data assuming that midnight 00:00:00 means the end of the day, you need to fix that.
If you need to correct your existing data, you need to add one day to every date in your database that has midnight as it's time component, (i.e., has a zero time component).
Update tbale set
date_time = dateAdd(day, 1, date_time)
Where date_time = dateadd(day, datediff(day, 0, date_time), 0)

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