I am trying to convert a field with positive and negative seconds to HH:MM:SS but failing in the process.
CONVERT(VARCHAR(6), RPAYCODE.TIMEINSECONDS/3600) +
':' +
RIGHT('0' + CONVERT(VARCHAR(2), (RPAYCODE.TIMEINSECONDS % 3600) / 60), 2)
AS "Pay Code Hrs"
When the seconds are positive it works fine = 00:30:00
When the seconds are negative I get this = 0:0*
This should handle the negative values:
CASE
WHEN #time_in_seconds < 0 THEN '-'
ELSE ''
END +
CONVERT(VARCHAR(6), ABS(RPAYCODE.TIMEINSECONDS)/3600) + ':' +
RIGHT('0' + CONVERT(VARCHAR(2), (ABS(RPAYCODE.TIMEINSECONDS) % 3600)/60), 2) AS [Pay Code Hrs]
Lean and simple conversion
Skip all those unnecessary calculations and string operations:
CASE WHEN RPAYCODE.TIMEINSECONDS < 0 THEN '-' ELSE '' END -- sign
+ CONVERT(varchar, DATEADD(s, ABS(RPAYCODE.TIMEINSECONDS), 0), 108) -- hh:mm:ss
Number format 108 is hh:mm:ss.
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).
The code below is doing just what i want it to do, as far as converting the time to a format that works for my needs. However, when the minutes and seconds are in the single digit, the format is not looking good. I would like to adjust the code to pad the minutes and seconds where it is needed. Any suggestions? Again, i would like to stay with the existing code as much as possible.
Thank you!
SELECT SUBSTRING(CONVERT(CHAR(14), DateTimeRaw, 100), 13, 2) + ':' +
CONVERT(nvarchar, DATEPART(minute, DateTimeRaw)) + ':' +
CONVERT(nvarchar, DATEPART(second,
DateTimeRaw)) + '.' + CONVERT(nvarchar, DATEPART(ms, DateTimeRaw) / 100)
+ ' ' + CONVERT(nvarchar, CASE WHEN datepart(hour, DateTimeRaw)
< 12 THEN 'AM' ELSE 'PM' END) AS AGMPLUSMSTIME
FROM RAW
RIGHT('00' + CONVERT(nvarchar, DATEPART(minute, DateTimeRaw)), 2)
Basically: concatenate 2 zeroes with your time value, ending up with something like 001, 0023, or 0059, then take the 2 right-most characters, leaving you with 01, 23, 59 respectively.
How about this:
select right(stuff(convert(varchar(26), getdate(), 9), 23,2, ' '), 13)
Result:
4:35:15:4 PM
I used getdate() instead of your field, just as an example
I'd like the sum of TotalVisitedTime time but it is alias column so how can I gtt it?
Select CONVERT(varchar(6), DATEDIFF(second, [Start], [End])/3600)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, [Start], [End]) % 3600) / 60), 2)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), DATEDIFF(second, [Start], [End]) % 60), 2) AS TotalVisitedTime
FROM [Table Name]
Note: [Start] and [End] are columns name.
SELECT SUM(TotalVisitedTime)
FROM (
-- AS PER #Alex Aza, #Andriy M
SELECT 1234 AS TotalVisitedTime
) AS OuterTable
I think it makes sense to sum up seconds and then do the conversion you are trying to do.
SELECT CONVERT(varchar(6), diff/3600)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), (diff % 3600) / 60), 2)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), diff % 60), 2) AS TotalVisitedTime
FROM
(
select sum(DATEDIFF(second, [Start], [End]) diff
from [Table Name]
) tt
What is the DATE FORMAT CODE for "yyyy.mm.dd.hh.mm.ss"?
I know that 34 (date format code) is "yyyymmddhhmmss", but what about the code for "yyyy.mm.dd.hh.mm.ss"?
This is on SQL 2005.
CAST and CONVERT on MSDN says "no".
You have to CONVERT twice with styles 102 and 108, with a concatenation and REPLACE.
Where did you get the "34" date format code from?
As gbn said, using one of the existing formats with some string concatenation would work. Another option is:
SELECT
CAST(YEAR(my_date) AS CHAR(4)) + '.' +
RIGHT('0' + CAST(MONTH(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DAY(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(HOUR, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(MINUTE, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(SECOND, my_date) AS VARCHAR(2)), 2)
Considering
SELECT CONVERT(VARCHAR(16), GETDATE(), 120) AS [YYYY-MM-DD]
---returns--- yyyy-mm-dd hh:ss
and you can use REPLACE to convert strings
REPLACE('a b c',' ','.')
----returns--- a.b.c
and you can recursively stack things you get to this
Select (
replace((replace((replace(CONVERT(VARCHAR(16), GETDATE(), 120),' ','.')), ':', '.')), '-', '.')
)
which returns: yyyy.mm.dd.hh.ss
works great for datetime stamps or filenames!