SQL Reporting Services Daylight saving time query - sql

I have been tasked with writting a report from MS Dynamic CRM. The report contains appointment and account information. I am using Visual Studio 2005 and SQL 2005.
The problem I have found is that if an appointment has a scheduled start date that falls after Britsh Summer Time but has a creation date before (BST) then the ScheduledStart field of the dbo.Appointment DB udates with the offset time + or - on hour. This means when I look for an appointment using reporting services and key in the time the appointment is due to start it returns no results because in the DB the time is either one hour earlier or later. I can manually amend the time for my search but this then returns the incorrect time on the report.
This will of course ony happen twice a year as appointments are only arranged a couple of weeks in advance but it is still a pain!
Is there a way using Transact SQL (or any method availbale to SSRS) I can allow for daylight saving times so even though the DB shows a Sceduled Start (dbo.Appointment.ScheduledStart) of say 11:00:00 the appointment is actually due to start at 10:00:00?

Alternately this case statement will work inside your existing query.
SELECT CASE
WHEN ([Created_Date] BETWEEN
Dateadd(yy, Datediff(yy, 0, [Scheduled_Date]), 0)
AND
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-03-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 4) % 7)) + ' 01:00:00', 20))
AND
([ScheduledDate] BETWEEN
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-03-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 4) % 7)) + ' 01:00:00', 20)
AND
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-10-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 1) % 7)) + ' 00:00:00', 20))
THEN Dateadd(hh, 1, [Scheduled_Date])
ELSE [Scheduled_Date]
END AS [Scheduled_Date]

CREATE FUNCTION OffsetBST (#CreatedDateTime DATETIME,
#ScheduledDateTime DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE #InDateYear VARCHAR(4)
DECLARE #StartDay VARCHAR(2)
DECLARE #EndDay VARCHAR(2)
DECLARE #BSTStart DATETIME
DECLARE #BSTEnd DATETIME
DECLARE #OffsetDateTime DATETIME
SET #StartDay = Convert(VARCHAR(2), (31 - (5 * Year(#CreatedDateTime)/4 + 4) % 7))
SET #EndDay = Convert(VARCHAR(2), (31 - (5 * Year(#CreatedDateTime)/4 + 1) % 7))
SET #InDateYear = Convert(VARCHAR(4), Year(#CreatedDateTime))
SET #BSTStart = Convert(DATETIME, #InDateYear + '-03-' + #StartDay + ' 01:00:00', 20)
SET #BSTEnd = Convert(DATETIME, #InDateYear + '-10-' + #EndDay + ' 00:00:00', 20)
IF (#CreatedDateTime BETWEEN DATEADD(yy, DATEDIFF(yy,0,#ScheduledDateTime), 0) AND #BSTStart)
AND (#ScheduledDateTime BETWEEN #BSTStart AND #BSTEnd)
SET #OffsetDateTime = Dateadd(hh, 1, #ScheduledDateTime)
ELSE
SET #OffsetDateTime = Dateadd(hh, 0, #ScheduledDateTime)
RETURN #OffsetDateTime
END
You can use this in your data source select statement. You will provide it with the created date and scheduled date and it will determine if the scheduled date requires a BST offset.
Like so:
SELECT A.Appointment_ID, A.Appointment_Name, dbo.OffsetBST(A.Created_Date,
A.Scheduled_Date) as Scheduled_Date
FROM AppointmentsTable as A

Related

Converting SQL to MariaDB statement

I’m trying to convert the below to MariaDB in order to get the next relevant date from today’s date.
The below SQL will get the next Sunday from the specified date
DECLARE #NextDayID INT;
SET #NextDayID = 1; -- Next Sunday
SELECT DATEADD(DAY, (DATEDIFF(DAY, ((#NextDayID + 5) % 7), GETDATE()) / 7) * 7 + 7, ((#NextDayID + 5) % 7)) AS NextDay
You can use date arithmetic:
select curdate() + interval (7 - dayofweek(curdate()) + 1) % 7 day

Calculate TIME IN ACCESS more than 24hours

I don't know if it's the right place to ask my question but I can't find anything else related in the Website.
I have an Access database that creates reports.
I have create queries and I want to calculate time more than 24 hours.
I have use function
TimeSerial((Sum(Minute(Table1.Field1)+Hour(Table1.Field1)*60)\60),(Sum(Minute(Table1.Field1)+Hour(Table1.Field1)*60) Mod 60),0) AS test
but the problem is that when the hours is more than 24 start from the beginning.
For example if I have a sum of 27:30 hours the result from my query will be
3:30.
Use DATEDIFF function:
Example:
DATEDIFF("h", start_date, end_date)
More info here: http://www.techonthenet.com/access/functions/date/datediff.php
Try something like this:
DECLARE #d1 DATETIME
DECLARE #d2 DATETIME
SET #d1 = '6/1/2014 12:15'
SET #d2 = '6/5/2014 18:22'
SELECT DATEDIFF(minute, #d1, #d2)
SELECT CAST((DATEDIFF(minute, #d1, #d2) / 60) AS VARCHAR(8)) + ':' +
right('0' + CAST((DATEDIFF(minute, #d1, #d2) % 60) AS VARCHAR(2)),2)

DATEDIFF in HH:MM:SS format

I need to calculate the total length in terms of Hours, Minutes, Seconds, and the average length, given some data with start time and end time.
For example the result must be something like 45:15:10 which means 45 hours 15 min 10 sec, or 30:07 for 30 min 07 sec.
We're using SQL Server 2008 R2 and the conversion failed when time is more than 24:59:59. Any idea of how I could do this?
For information, the columns in the table are Id, StartDateTime, EndDateTime, etc. I need to make a monthly report which contains the recordings count of the month, the total length of these records, and the average length. I'd like to know if there is an easy way to perform all of this.
You shouldn't be converting to time - it is meant to store a point in time on a single 24h clock, not a duration or interval (even one that is constrained on its own to < 24 hours, which clearly your data is not). Instead you can take the datediff in the smallest interval required (in your case, seconds), and then perform some math and string manipulation to present it in the output format you need (it might also be preferable to return the seconds to the application or report tool and have it do this work).
DECLARE #d TABLE
(
id INT IDENTITY(1,1),
StartDateTime DATETIME,
EndDateTime DATETIME
);
INSERT #d(StartDateTime, EndDateTime) VALUES
(DATEADD(DAY, -2, GETDATE()), DATEADD(MINUTE, 15, GETDATE())),
(GETDATE() , DATEADD(MINUTE, 22, GETDATE())),
(DATEADD(DAY, -1, GETDATE()), DATEADD(MINUTE, 5, GETDATE())),
(DATEADD(DAY, -4, GETDATE()), DATEADD(SECOND, 14, GETDATE()));
;WITH x AS (SELECT id, StartDateTime, EndDateTime,
d = DATEDIFF(SECOND, StartDateTime, EndDateTime),
a = AVG(DATEDIFF(SECOND, StartDateTime, EndDateTime)) OVER()
FROM #d
)
SELECT id, StartDateTime, EndDateTime,
[delta_HH:MM:SS] = CONVERT(VARCHAR(5), d/60/60)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), d/60%60), 2)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), d % 60), 2),
[avg_HH:MM:SS] = CONVERT(VARCHAR(5), a/60/60)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), a/60%60), 2)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), a % 60), 2)
FROM x;
Results:
id StartDateTime EndDateTime delta_HH:MM:SS avg_HH:MM:SS
-- ------------------- ------------------- -------------- ------------
1 2013-01-19 14:24:46 2013-01-21 14:39:46 48:15:00 42:10:33
2 2013-01-21 14:24:46 2013-01-21 14:46:46 0:22:00 42:10:33
3 2013-01-20 14:24:46 2013-01-21 14:29:46 24:05:00 42:10:33
4 2013-01-17 14:24:46 2013-01-21 14:25:00 96:00:14 42:10:33
This isn't precisely what you asked for, as it won't show just MM:SS for deltas < 1 hour. You can adjust that with a simple CASE expression:
;WITH x AS (SELECT id, StartDateTime, EndDateTime,
d = DATEDIFF(SECOND, StartDateTime, EndDateTime),
a = AVG(DATEDIFF(SECOND, StartDateTime, EndDateTime)) OVER()
FROM #d
)
SELECT id, StartDateTime, EndDateTime,
[delta_HH:MM:SS] = CASE WHEN d >= 3600 THEN
CONVERT(VARCHAR(5), d/60/60) + ':' ELSE '' END
+ RIGHT('0' + CONVERT(VARCHAR(2), d/60%60), 2)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), d % 60), 2),
[avg_HH:MM:SS] = CASE WHEN a >= 3600 THEN
CONVERT(VARCHAR(5), a/60/60) + ':' ELSE '' END
+ RIGHT('0' + CONVERT(VARCHAR(2), a/60%60), 2)
+ ':' + RIGHT('0' + CONVERT(VARCHAR(2), a % 60), 2)
FROM x;
This query changes the delta column in the 2nd row in the above result from 0:22:00 to 22:00.
I slightly modified Avinash's answer as it may end with error if difference is too big. If you need only HH:mm:ss it is sufficient to distinguish at seconds level ony like this:
SELECT CONVERT(time,
DATEADD(s,
DATEDIFF(s,
'2018-01-07 09:53:00',
'2018-01-07 11:53:01'),
CAST('1900-01-01 00:00:00.0000000' as datetime2)
)
)
SELECT CONVERT(time,
DATEADD(mcs,
DATEDIFF(mcs,
'2007-05-07 09:53:00.0273335',
'2007-05-07 09:53:01.0376635'),
CAST('1900-01-01 00:00:00.0000000' as datetime2)
)
)
If you want to do averages, then the best approach is to convert to seconds or fractions of a day. Day fractions are convenient in SQL Server, because you can do things like:
select avg(cast(endtime - starttime) as float)
from t
You can convert it back to a datetime using the reverse cast:
select cast(avg(cast(endtime - starttime as float) as datetime)
from t
The arithmetic to get the times in the format you want . . . that is a pain. You might consider including days in the final format, and using:
select right(convert(varchar(255), <val>, 120), 10)
To get the hours exceeding 24, here is another approach:
select cast(floor(cast(<val> as float)*24) as varchar(255))+right(convert(varchar(255), <val>, 120), 6)
It uses convert for minutes and seconds, which should be padded with 0s on the left. It then appends the hours as a separate value.
Starting in SQL SERVER 2012, you don't need to use DATEDIFF function. You can use FORMAT function to achieve what you want:
SELECT
FORMAT(CONVERT(TIME, [appoitment].[Start] - [appointment].[End]), N'hh\:mm') AS 'Duration'
FROM
[tblAppointment] (NOLOCK)
A way that avoids overflows and can include days and go all the way to milliseconds in the output:
DECLARE #startDate AS DATETIME = '2018-06-01 14:20:02.100'
DECLARE #endDate AS DATETIME = '2018-06-02 15:23:09.000'
SELECT CAST(DATEDIFF(day,'1900-01-01', #endDate - #startDate) AS VARCHAR) + 'd ' + CONVERT(varchar(22), #endDate - #startDate, 114)
The above will return
1d 01:03:06:900
And, off course, you can use the formatting of your choice
SQL Supports datetime substraction which outputs a new datetime relative to the MIN date (for instance 1900-01-01, you can probably get this value from some system variable) This works better than DATEDIFF, because DATEDIFF will count ONE for each "datepart boundaries crossed", even if the elapsed time is less than a whole datapart. Another nice thing about this method is that it allows you to use the date formatting conversions.
If days is the (positive) number of days, like 0.5 for 12 hours, use this expression to format it as a proper duration:
CONVERT(varchar(9), FLOOR(days * 24)) + RIGHT(CONVERT(char(19), CAST(days AS datetime), 120), 6)
Excel will understands values up to 9999:59:59 when pasted. There apply a custom format: [h]:mm:ss in the English version ([u]:mm:ss for Dutch).

Formatting a date/time in SQL Server 2005

I have a datetime field in a SQL Server 2005 table that has values like this:
2012-04-23 09:00:00.000
2012-04-23 14:00:00.000
The minutes, seconds, and microseconds are always zero.
I need to display a "time slot" (basically, the time plus one hour) like this:
2012/04/23 09:00 AM - 10:00 AM
2012/04/23 02:00 PM - 03:00 PM
I got what I needed using this:
SELECT SUBSTRING(CONVERT(VARCHAR, TimeSlot, 20), 1, 10) + ' ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), TimeSlot, 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), TimeSlot, 100),18,2) + ' - ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), DATEADD(hh, 1, TimeSlot), 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), DATEADD(hh, 1, TimeSlot), 100), 18, 2) AS TimeSlot
FROM MyTable
It works, but I feel so dirty.
I know I could do it easier in the code (VB .NET), but assume I have to do it in SQL.
Is there any cleaner way to do this?
Taking the built-in conversions, I would use something like this:
SELECT
convert(varchar, [TimeSlot], 111) + ' ' +
RIGHT(convert(varchar, [TimeSlot], 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, [TimeSlot]), 0), 7)
FROM
[MyTable];
this gives you the line you want as
2012/04/23 9:00AM - 10:00AM
in detail:
convert(varchar, #dt, 111) converts the datetime to the Japanese format yy/mm/dd
convert(varchar, #dt, 0) converts the datetime to Apr 23 2012 9:00AM so we can use the time part
dateadd(hour, 1, #dt) adds one hour to the current datetime value
you can test it without tables with:
DECLARE #dt DATETIME
SET #dt = '2012-04-23 09:00:00'
PRINT convert(varchar, #dt, 111) + ' ' +
RIGHT(convert(varchar, #dt, 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, #dt), 0), 7);
The built in date/time formats in SQL Server will only format to an actual date format, so you're up for custom formatting anyway. If you're feeding SSRS with this then you might be able to use an expression to format it within SSRS. Otherwise you're up for formatting it with the query using something like the technique you're describing.
If you're actually displaying it with a VB app then you could do that client-side.
If your date/time values are always on the hour or some consistent interval you could also make a reference table keyed on the time with the formatted interval stored as a varchar column and use that. That would save you in-lining the formatting expression into every query that needed to produce the interval description.
FollowingConcernedOfTunbridgeW's suggestion, if you maintain a two-column table with a primary key int column [hr] that goes from 0 to 23 and a char(20) column [rng] containing the space-prefixed range string for each hour
' 12:00 AM - 01:00 AM' (in the row where hr = 0)
...
' 11:00 PM - 12:00 AM' (in the row where hr = 23)
this simply query should produce what you want:
select
convert(char(10),TimeSlot, 111) + rng as TimeRange
from T join RangesByHr
on datepart(hour,TimeSlot) = hr;

Difference of two date time in sql server

Is there any way to take the difference between two datetime in sql server?
For example, my dates are
2010-01-22 15:29:55.090
2010-01-22 15:30:09.153
So, the result should be 14.063 seconds.
Just a caveat to add about DateDiff, it counts the number of times you pass the boundary you specify as your units, so is subject to problems if you are looking for a precise timespan.
e.g.
select datediff (m, '20100131', '20100201')
gives an answer of 1, because it crossed the boundary from January to February, so even though the span is 2 days, datediff would return a value of 1 - it crossed 1 date boundary.
select datediff(mi, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153')
Gives a value of 1, again, it passed the minute boundary once, so even though it is approx 14 seconds, it would be returned as a single minute when using Minutes as the units.
SELECT DATEDIFF (MyUnits, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')
Substitute "MyUnits" based on DATEDIFF on MSDN
SELECT DATEDIFF(day, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')
Replace day with other units you want to get the difference in, like second, minute etc.
I can mention four important functions of MS SQL Server that can be very useful:
1) The function DATEDIFF() is responsible to calculate differences between two dates, the result could be "year quarter month dayofyear day week hour minute second millisecond microsecond nanosecond", specified on the first parameter (datepart):
select datediff(day,'1997-10-07','2011-09-11')
2) You can use the function GETDATE() to get the actual time and calculate differences of some date and actual date:
select datediff(day,'1997-10-07', getdate() )
3) Another important function is DATEADD(), used to convert some value in datetime using the same datepart of the datediff, that you can add (with positive values) or substract (with negative values) to one base date:
select DATEADD(day, 45, getdate()) -- actual datetime adding 45 days
select DATEADD( s,-638, getdate()) -- actual datetime subtracting 10 minutes and 38 seconds
4) The function CONVERT() was made to format the date like you need, it is not parametric function, but you can use part of the result to format the result like you need:
select convert( char(8), getdate() , 8) -- part hh:mm:ss of actual datetime
select convert( varchar, getdate() , 112) -- yyyymmdd
select convert( char(10), getdate() , 20) -- yyyy-mm-dd limited by 10 characters
DATETIME cold be calculated in seconds and one interesting result mixing these four function is to show a formated difference um hours, minutes and seconds (hh:mm:ss) between two dates:
declare #date1 datetime, #date2 datetime
set #date1=DATEADD(s,-638,getdate())
set #date2=GETDATE()
select convert(char(8),dateadd(s,datediff(s,#date1,#date2),'1900-1-1'),8)
... the result is 00:10:38 (638s = 600s + 38s = 10 minutes and 38 seconds)
Another example:
select distinct convert(char(8),dateadd(s,datediff(s, CRDATE , GETDATE() ),'1900-1-1'),8) from sysobjects order by 1
I tried this way and it worked. I used SQL Server version 2016
SELECT DATEDIFF(MILLISECOND,'2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')/1000.00;
Different DATEDIFF Functions are:
SELECT DATEDIFF(year, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(quarter, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(dayofyear, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(week, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(hour, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(minute, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(second, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(millisecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
Ref: https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017
Ok we all know the answer involves DATEDIFF(). But that gives you only half the result you may be after. What if you want to get the results in human-readable format, in terms of Minutes and Seconds between two DATETIME values?
The CONVERT(), DATEADD() and of course DATEDIFF() functions are perfect for a more easily readable result that your clients can use, instead of a number.
i.e.
CONVERT(varchar(5), DATEADD(minute, DATEDIFF(MINUTE, date1, date2), 0), 114)
This will give you something like:
HH:MM
If you want more precision, just increase the VARCHAR().
CONVERT(varchar(12), DATEADD(minute, DATEDIFF(MINUTE, date1, date2), 0), 114)
HH:MM.SS.MS
There are a number of ways to look at a date difference, and more when comparing date/times. Here's what I use to get the difference between two dates formatted as "HH:MM:SS":
ElapsedTime AS
RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) / 3600 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2)
I used this for a calculated column, but you could trivially rewrite it as a UDF or query calculation. Note that this logic rounds down fractional seconds; 00:00.00 to 00:00.999 is considered zero seconds, and displayed as "00:00:00".
If you anticipate that periods may be more than a few days long, this code switches to D:HH:MM:SS format when needed:
ElapsedTime AS
CASE WHEN DATEDIFF(S, StartDate, EndDate) >= 359999
THEN
CAST(DATEDIFF(S, StartDate, EndDate) / 86400 AS VARCHAR(7)) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 86400 / 3600 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2)
ELSE
RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) / 3600 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 3600 / 60 AS VARCHAR(2)), 2) + ':'
+ RIGHT('0' + CAST(DATEDIFF(S, StartDate, EndDate) % 60 AS VARCHAR(2)), 2)
END
The following query should give the exact stuff you are looking out for.
select datediff(second, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153')
Here is the link from MSDN for what all you can do with datediff function .
https://msdn.microsoft.com/en-us/library/ms189794.aspx
Internally in SQL Server dates are stored as 2 integers. The first integer is the number of days before or after the base date (1900/01/01). The second integer stores the number of clock ticks after midnight, each tick is 1/300 of a second.
More info here
Because of this, I often find the simplest way to compare dates is to simply substract them. This handles 90% of my use cases. E.g.,
select date1, date2, date2 - date1 as DifferenceInDays
from MyTable
...
When I need an answer in units other than days, I will use DateDiff.
SELECT DATEDIFF(yyyy, '2011/08/25', '2017/08/25') AS DateDiff
It's gives you difference between two dates in Year
Here (2017-2011)=6 as a result
Syntax:
DATEDIFF(interval, date1, date2)
Use This for DD:MM:SS:
SELECT CONVERT(VARCHAR(max), Datediff(dd, '2019-08-14 03:16:51.360',
'2019-08-15 05:45:37.610'))
+ ':'
+ CONVERT(CHAR(8), Dateadd(s, Datediff(s, '2019-08-14 03:16:51.360',
'2019-08-15 05:45:37.610'), '1900-1-1'), 8)
So this isn't my answer but I just found this while searching around online for a question like this as well. This guy set up a procedure to calculate hours, minutes and seconds. The link and the code:
--Creating Function
If OBJECT_ID('UFN_HourMinuteSecond') Is Not Null
Drop Function dbo.UFN_HourMinuteSecond
Go
Exec(
'Create Function dbo.UFN_HourMinuteSecond
(
#StartDateTime DateTime,
#EndDateTime DateTime
) Returns Varchar(10)
As
Begin
Declare #Seconds Int,
#Minute Int,
#Hour Int,
#Elapsed Varchar(10)
Select #Seconds = ABS(DateDiff(SECOND ,#StartDateTime,#EndDateTime))
If #Seconds >= 60
Begin
select #Minute = #Seconds/60
select #Seconds = #Seconds%60
If #Minute >= 60
begin
select #hour = #Minute/60
select #Minute = #Minute%60
end
Else
Goto Final
End
Final:
Select #Hour = Isnull(#Hour,0), #Minute = IsNull(#Minute,0), #Seconds = IsNull(#Seconds,0)
select #Elapsed = Cast(#Hour as Varchar) + '':'' + Cast(#Minute as Varchar) + '':'' + Cast(#Seconds as Varchar)
Return (#Elapsed)
End'
)
declare #dt1 datetime='2012/06/13 08:11:12', #dt2 datetime='2012/06/12 02:11:12'
select CAST((#dt2-#dt1) as time(0))
PRINT DATEDIFF(second,'2010-01-22 15:29:55.090','2010-01-22 15:30:09.153')
select
datediff(millisecond,'2010-01-22 15:29:55.090','2010-01-22 15:30:09.153') / 1000.0 as Secs
result:
Secs
14.063
Just thought I'd mention it.
Sol-1:
select
StartTime
, EndTime
, CONVERT(NVARCHAR,(EndTime-StartTime), 108) as TimeDiff
from
[YourTable]
Sol-2:
select
StartTime
, EndTime
, DATEDIFF(hh, StartTime, EndTime)
, DATEDIFF(mi, StartTime, EndTime) % 60
from
[YourTable]
Sol-3:
select
DATEPART(hour,[EndTime]-[StartTime])
, DATEPART(minute,[EndTime]-[StartTime])
from
[YourTable]
Datepart works the best
Please check below trick to find the date difference between two dates
DATEDIFF(DAY,ordr.DocDate,RDR1.U_ProgDate) datedifff
where you can change according your requirement as you want difference of days or month or year or time.
CREATE FUNCTION getDateDiffHours(#fdate AS datetime,#tdate as datetime)
RETURNS varchar (50)
AS
BEGIN
DECLARE #cnt int
DECLARE #cntDate datetime
DECLARE #dayDiff int
DECLARE #dayDiffWk int
DECLARE #hrsDiff decimal(18)
DECLARE #markerFDate datetime
DECLARE #markerTDate datetime
DECLARE #fTime int
DECLARE #tTime int
DECLARE #nfTime varchar(8)
DECLARE #ntTime varchar(8)
DECLARE #nfdate datetime
DECLARE #ntdate datetime
-------------------------------------
--DECLARE #fdate datetime
--DECLARE #tdate datetime
--SET #fdate = '2005-04-18 00:00:00.000'
--SET #tdate = '2005-08-26 15:06:07.030'
-------------------------------------
DECLARE #tempdate datetime
--setting weekends
SET #fdate = dbo.getVDate(#fdate)
SET #tdate = dbo.getVDate(#tdate)
--RETURN #fdate
SET #fTime = datepart(hh,#fdate)
SET #tTime = datepart(hh,#tdate)
--RETURN #fTime
if datediff(hour,#fdate, #tdate) <= 9
RETURN(convert(varchar(50),0) + ' Days ' + convert(varchar(50),datediff(hour,#fdate, #tdate))) + ' Hours'
else
--setting working hours
SET #nfTime = dbo.getV00(convert(varchar(2),datepart(hh,#fdate))) + ':' +dbo.getV00(convert(varchar(2),datepart(mi,#fdate))) + ':'+ dbo.getV00(convert(varchar(2),datepart(ss,#fdate)))
SET #ntTime = dbo.getV00(convert(varchar(2),datepart(hh,#tdate))) + ':' +dbo.getV00(convert(varchar(2),datepart(mi,#tdate))) + ':'+ dbo.getV00(convert(varchar(2),datepart(ss,#tdate)))
IF #fTime > 17
begin
set #nfTime = '17:00:00'
end
else
begin
IF #fTime < 8
set #nfTime = '08:00:00'
end
IF #tTime > 17
begin
set #ntTime = '17:00:00'
end
else
begin
IF #tTime < 8
set #ntTime = '08:00:00'
end
-- used for working out whole days
SET #nfdate = dateadd(day,1,#fdate)
SET #ntdate = #tdate
SET #nfdate = convert(varchar,datepart(yyyy,#nfdate)) + '-' + convert(varchar,datepart(mm,#nfdate)) + '-' + convert(varchar,datepart(dd,#nfdate))
SET #ntdate = convert(varchar,datepart(yyyy,#ntdate)) + '-' + convert(varchar,datepart(mm,#ntdate)) + '-' + convert(varchar,datepart(dd,#ntdate))
SET #cnt = 0
SET #dayDiff = 0
SET #cntDate = #nfdate
SET #dayDiffWk = convert(decimal(18,2),#ntdate-#nfdate)
--select #nfdate,#ntdate
WHILE #cnt < #dayDiffWk
BEGIN
IF (NOT DATENAME(dw, #cntDate) = 'Saturday') AND (NOT DATENAME(dw, #cntDate) = 'Sunday')
BEGIN
SET #dayDiff = #dayDiff + 1
END
SET #cntDate = dateadd(day,1,#cntDate)
SET #cnt = #cnt + 1
END
--SET #dayDiff = convert(decimal(18,2),#ntdate-#nfdate) --datediff(day,#nfdate,#ntdate)
--SELECT #dayDiff
set #fdate = convert(varchar,datepart(yyyy,#fdate)) + '-' + convert(varchar,datepart(mm,#fdate)) + '-' + convert(varchar,datepart(dd,#fdate)) + ' ' + #nfTime
set #tdate = convert(varchar,datepart(yyyy,#tdate)) + '-' + convert(varchar,datepart(mm,#tdate)) + '-' + convert(varchar,datepart(dd,#tdate)) + ' ' + #ntTime
set #markerFDate = convert(varchar,datepart(yyyy,#fdate)) + '-' + convert(varchar,datepart(mm,#fdate)) + '-' + convert(varchar,datepart(dd,#fdate)) + ' ' + '17:00:00'
set #markerTDate = convert(varchar,datepart(yyyy,#tdate)) + '-' + convert(varchar,datepart(mm,#tdate)) + '-' + convert(varchar,datepart(dd,#tdate)) + ' ' + '08:00:00'
--select #fdate,#tdate
--select #markerFDate,#markerTDate
set #hrsDiff = convert(decimal(18,2),datediff(hh,#fdate,#markerFDate))
--select #hrsDiff
set #hrsDiff = #hrsDiff + convert(int,datediff(hh,#markerTDate,#tdate))
--select #fdate,#tdate
IF convert(varchar,datepart(yyyy,#fdate)) + '-' + convert(varchar,datepart(mm,#fdate)) + '-' + convert(varchar,datepart(dd,#fdate)) = convert(varchar,datepart(yyyy,#tdate)) + '-' + convert(varchar,datepart(mm,#tdate)) + '-' + convert(varchar,datepart(dd,#tdate))
BEGIN
--SET #hrsDiff = #hrsDiff - 9
Set #hrsdiff = datediff(hour,#fdate,#tdate)
END
--select FLOOR((#hrsDiff / 9))
IF (#hrsDiff / 9) > 0
BEGIN
SET #dayDiff = #dayDiff + FLOOR(#hrsDiff / 9)
SET #hrsDiff = #hrsDiff - FLOOR(#hrsDiff / 9)*9
END
--select convert(varchar(50),#dayDiff) + ' Days ' + convert(varchar(50),#hrsDiff) + ' Hours'
RETURN(convert(varchar(50),#dayDiff) + ' Days ' + convert(varchar(50),#hrsDiff)) + ' Hours'
END
For Me This worked Perfectly
Convert(varchar(8),DATEADD(SECOND,DATEDIFF(SECOND,LogInTime,LogOutTime),0),114)
and the Output is
HH:MM:SS which is shown accurately in my case.
Please try
DECLARE #articleDT DATETIME;
DECLARE #nowDate DATETIME;
-- Time of the ARTICLE created
SET #articleDT = '2012-04-01 08:10:16';
-- Simulation of NOW datetime
-- (in real world you would probably use GETDATE())
SET #nowDate = '2012-04-10 11:35:36';
-- Created 9 days ago.
SELECT 'Created ' + CAST(DATEDIFF(day, #articleDT, #nowDate) AS NVARCHAR(50)) + ' days ago.';
-- Created 1 weeks, 2 days, 3 hours, 25 minutes and 20 seconds ago.
SELECT 'Created '
+ CAST(DATEDIFF(second, #articleDT, #nowDate) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks, '
+ CAST(DATEDIFF(second, #articleDT, #nowDate) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days, '
+ CAST(DATEDIFF(second, #articleDT, #nowDate) / 60 / 60 % 24 AS NVARCHAR(50)) + ' hours, '
+ CAST(DATEDIFF(second, #articleDT, #nowDate) / 60 % 60 AS NVARCHAR(50)) + ' minutes and '
+ CAST(DATEDIFF(second, #articleDT, #nowDate) % 60 AS NVARCHAR(50)) + ' seconds ago.';
For MS SQL, you can convert the datetime value to a double value.
The integer part contains the number of days since 1900-01-01, the fractional part contains the time in hours.
So you can calculate date difference as:
cast(date1-date2 as FLOAT)