Convert ISO8061 Duration to a datetime or time value - sql-server

I have some ISO8601 Durations (not to be confused with ISO601 datetime)
Here are some example valid values:
P1D
PT0H
PT11M
P1DT2H15M
PT10H11M
PT2H46M12S
the specification is here:
https://en.wikipedia.org/wiki/ISO_8601#Durations
Ideally, I would like to take these values and parse them as either time or datetime2 values
to make it easier to work with
I was able to brute force parse these using string functions but the code is complex and seems error prone hoping there was a better way?
with anchors as
(
SELECT
dt[duration]
, NULLIF(CHARINDEX('D', dt.duration), 0) As DLocation
, NULLIF(CHARINDEX('T', dt.duration), 0) As TLocation
, NULLIF(CHARINDEX('H', dt.duration), 0) As HLocation
, NULLIF(CHARINDEX('M', dt.duration), 0) As MLocation
, NULLIF(CHARINDEX('S', dt.duration), 0) As SLocation
, LEN(dt.duration) as TotalLength
FROM dbo.DurationTest dt
)
SELECT
duration
,DaysValue = CAST(ISNULL(SUBSTRING(duration, 2, (DLocation - 2)), 0) as tinyint)
,HoursValue = CAST(ISNULL(SUBSTRING(duration, TLocation + 1, (HLocation - TLocation) - 1 ), 0) as tinyint)
,MinutesValue = CAST(ISNULL(SUBSTRING(duration, COALESCE(HLocation, TLocation) + 1, MLocation - COALESCE(HLocation, TLocation) - 1), 0) as tinyint)
,SecondsValue = CAST(ISNULL(SUBSTRING(duration, COALESCE(MLocation, TLocation) + 1, SLocation - COALESCE(MLocation, TLocation) - 1 ), 0) as tinyint)
FROM anchors
this code gets the values into days, hours, minutes and seconds. converting to either a int value to seconds or a datetime2 is pretty well documented from that. As pointed out in the comments a time data type will only work for values <24 so I've kind of given up on that.
for further context this data is coming from ADP payroll webservice and this field tracks the daily time entry you would think it would be less than 24hrs but I have some outliers in my dataset.
I've created the following enhancement request here (not sure if it will get traction or not):
https://feedback.azure.com/d365community/idea/557e5b51-1824-ed11-a81b-6045bd853198

Here's one working example. Read the header for what it does but this one returns the ADP "ISO-Like" durations as Decimal Hours to 6 Decimal Places. If you need something else instead, the changes will be quite simple to make. Let me know.
Here's the iTVF (inline Table Valued Function). Details are in the comments. As so often happens, the comments are bigger than the code itself.
CREATE FUNCTION dbo.AdpDurToDecHours
/*********************************************************************
Purpose:
Convert ADP payroll webservice daily time entries (durations) from a
subset of the ISO Duration notation to Decimal Hours.
*** DO NOT USE THIS FUNCTION IF YEAR AND/OR MONTH CAN BE PRESENT! ***
----------------------------------------------------------------------
Usage Examples:
--===== Convert a set of ADP time entries from a table.
SELECT st.SomeOtherColumns
,adp.DecimalHours
FROM dbo.SomeTable st
CROSS APPLY dbo.AdpDurToDecHours(st.AdpDur) adp
;
--===== Convert a single value from a variable.
SELECT adp.DecimalHours FROM dbo.AdpDurToDecHours(#AdpDur) adp
;
----------------------------------------------------------------------
Dependencies:
1. None
----------------------------------------------------------------------
Programmer Notes:
1. DO NOT USE THIS FUNCTION IF YEAR AND/OR MONTH CAN BE PRESENT!!!
2. This code will fail with the following error if you pass it a
NULL, Empty String, or BLANK String. Other "non-ISO" compliant
values may also fail or create improper output but such other
things have not been tested. Ostensibly, the values passed should
be compliant for proper returns.
Msg 1014, Level 15, State 1, Line 30
A TOP or FETCH clause contains an invalid value.
3. This code follows the ISO specification for durations except for
"Year" and "Month", which have been intentionally excluded because
they're "indeterminate" for durations unless combined with a
starting date.
----------------------------------------------------------------------
References:
1. https://en.wikipedia.org/wiki/ISO_8601#Durations
2. https://stackoverflow.com/questions/73479091/convert-iso8061-duration-to-a-datetime-or-time-value
----------------------------------------------------------------------
Revision History:
Rev 00 - 25 Aug 2022 - Jeff Moden
- Create and test PoP code.
Rev 01 - 26 Aug 2022 - Jeff Moden
- Exclude "Year" and "Month".
- Convert output to Decimal Hours.
Rev 02 - 27 Aug 2022 - Jeff Moden
- Convert to documented function (iTVF).
*********************************************************************/
(#AdpDur VARCHAR(36))
RETURNS TABLE WITH SCHEMABINDING
RETURN WITH
--==== Creates an inline sequence generator
S1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1))S0(N))
,cteTally(N) AS (SELECT TOP(LEN(#AdpDur))
N = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM S1 a,S1) --1 to 36 rows max
,cteLocateDurTypes AS
(--==== Find positions/lagging positions of the duration type tokens
SELECT DurType = SUBSTRING(#AdpDur,t.N,1)
,LN = LAG(t.N,1,0) OVER (ORDER BY t.N)
,t.N
FROM cteTally t
WHERE SUBSTRING(#AdpDur,N,1) IN ('P','W','D','T','H','M','S')
)
,cteIsolateValue AS
(--==== Determine the value for each duration token present
SELECT DurType
,Value = CONVERT(INT,SUBSTRING(#AdpDur,LN+1,N-1-LN))
FROM cteLocateDurTypes
)--==== Convert DurType Values to seconds and sum as Decimal Hours
SELECT DecimalHours = SUM(
CASE DurType
WHEN 'S' THEN Value
WHEN 'M' THEN Value*60
WHEN 'H' THEN Value*3600
WHEN 'D' THEN Value*86400
WHEN 'W' THEN Value*604800
ELSE 0
END
)/3600.0
FROM cteIsolateValue
;
GO
Here's a short test table based on what #JasonHorner posted with a few additions.
--===== Drop the test table if it exists to make reruns easier in SSMS
DROP TABLE IF EXISTS #DurationTest;
GO
--===== Create and populate the test table on-the-fly.
SELECT Duration = CONVERT(VARCHAR(36),v.Duration)
INTO #DurationTest
FROM (VALUES
--(NULL) --Causes Failure
--,('') --Causes Failure
--,(' ') --Causes Failure
('PT') --It could happen
,('P1W') --Added this entry
,('P1D')
,('PT1H') --Added this entry
,('PT1M') --Added this entry
,('PT1S') --Added this entry
,('PT0H')
,('PT10H') --Added this entry
,('PT11M')
,('P1DT2H15M')
,('PT10H11M')
,('PT2H46M12S')
,('P1W2DT12H46M12S') --Added this entry
)v(Duration)
;
GO
Here's a test of the function using the test table as the source.
--===== Simple test
SELECT *
FROM #DurationTest dt
CROSS APPLY dbo.AdpDurToDecHours(dt.Duration) adp
;
GO
And, finally, here are the results from the test.

Related

Varbinary search with min and max pattern type

So I'm trying to make a query which goes through varbinary data. The issue is that I can't really finish what I'm trying to achieve. What you should know about the column is varbinary(50) and the patterns that occur have no specific order in writing, meaning every prefix could be anywhere as long it has 3 bytes(0x000000) First one is the prefix second and third are value data that I'm looking to check if its within the range i like. All the data is written like this.
What I've tried:
DECLARE #t TABLE (
val VARBINARY(MAX)
)
INSERT INTO #t SELECT 0x00000100000000000000000000000000000000000000000000000000
INSERT INTO #t SELECT 0x00001000000000000000000000000000000000000000000000000000
INSERT INTO #t SELECT 0x00010000000000000000000000000000000000000000000000000000
INSERT INTO #t SELECT 0x00100000000000000000000000000000000000000000000000000000
INSERT INTO #t SELECT 0x00000f00000000000000000000000000000000000000000000000000
declare #pattern varbinary(max)
declare #pattern2 varbinary(max)
set #pattern = 0x0001
set #pattern2 = #pattern+0xFF
select #pattern,#pattern2
SELECT
*
FROM #t
WHERE val<#pattern
OR val>#pattern2
This was total bust the patterns were accurate up to 2 symbols if I were to use 4 symbols as pattern it would work only if the pattern is in predefined position. I've tried combination of this and everything below.
WHERE CONVERT(varbinary(2), val) = 0xdata
also this:
select *
from table
where CONVERT(varchar(max),val,2) like '%data%'
Which works great for searching exact patterns, but not for ranges, I need some combination of both.
I'm aware I could technically add every possible outcome 1 by 1 and let it cycle through all the listed possibilities, but there has to be a smarter way.
Goals:
Locating the prefix(first binary data pair)
Defining a max value after the prefix, everything above that threshold to be listed in the results. Let's say '26' is the prefix, the highest allowed number after is '9600' or '269600'. Basically any data that exceeds this pattern '269600' should be detected example '269700'.
or query result would post this:
select * from table where CONVERT(varchar(max),attr,2) like
'%269700%'
I need something that would detect this on its own while i just give it start and end to look in between like the highest number variation would be '26ffff', but limiting it to something like 'ff00' is acceptable for what I'm looking for.
My best guess is 2 defined numbers, 1 being the allowed max range
and 2nd for a cap, so it doesn't go through every possible outcome.
But I would be happy to whatever works.
I'm aware this explanation is pretty dire, but bear with me, thanks.
*Update after the last suggestion
SELECT MIN(val), MAX(val) FROM #t where CONVERT(varchar(max),val,2) like '%26%'
This is pretty close, but its not sufficient i need to cycle through alot of data and use it after this would select only min or max even with the prefix filter. I believe i need min and max defined as a start and end range where the query should look for.
**Update2
I'm afraid you would end up disappointed, its nothing that interesting.
The data origin is related to a game server which stores the data like this. There's the predefined prefixes which are the stat type and the rest of the data is the actual numeric value of the stat. The data is represented by 6 characters data intervals. Here is a sample of the data stream. Its always 6-6-6-6-6 as long there's space to record the data on since its capped at 50 characters.
0x0329000414000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000
**Update3
Yes the groups are always in 3byte fashion, yes my idea was exactly that use the first byte to narrow down the search and use then use the second 2 bytes to filter it. I just don't know how to pull it off in an effective way. I'm not sure if i understood what u meant by predictively aligned assuming you meant if stat/prefix/header would always end up at the same binary location, if that's correct the answer is no. If the 3byte pattern is violated the data becomes unreadable meaning even if you don't need the extra byte you have to count it otherwise the data breaks example of a working data.
0x032900'041400'
example of a broken data:
0x0329'041400'
The only issue i could think is when the prefix and part of the value are both true example:
0x262600
Unless the query is specifically ordered to read the data in 3byte sequence meaning it knows that the first byte is always a prefix and the other 2 bytes are value.
Q:Can that be used as an alignment indicator so that the first non-zero byte after at least 3 zero bytes indicates the start of a group?
A:Yes, but that's unlikely I mean it although possible it would be written in order like:
0x260000'270000'
It wouldn't skip forward an entire 3byte group filled with no data. This type of entry would occur if someone were to manually insert it to the db, the server doesn't make records with gaps like those as far I'm aware:
0x260000'000000'270000'
To address your last comment that's something I don't know how to express it in a working query, except for the boneheaded version which would be me manually adding every possible number within my desired range with +1bit after that number. As you can imagine the query would look terrible. That's why I'm looking for a smarter solution that I cannot figure out how to do so by my self.
select * from #t
where (CONVERT(varchar(max),val,2) like '%262100%' or
CONVERT(varchar(max),attr,2) like '%262200%' or
etc...)
This may be a partial answer from which you can build.
The following will split the input data up into 3-byte (6 hex character) groups. It then extracts the first byte as the key, and several representations of the remaining two bytes as values.
SELECT S.*, P.*
FROM #t T
CROSS APPLY (
SELECT
N.Offset,
SUBSTRING(T.val, N.Offset + 1, 3) AS Segment
FROM (
VALUES
(0), (3), (6), (9), (12), (15), (18), (21), (24), (27),
(30), (33), (36), (39)
) N(Offset)
WHERE N.Offset < LEN(T.val) - 3
) S
CROSS APPLY(
SELECT
CONVERT(TINYINT, SUBSTRING(S.Segment, 1, 1)) AS [Key],
CONVERT(TINYINT, SUBSTRING(S.Segment, 2, 1)) AS [Value1],
CONVERT(TINYINT, SUBSTRING(S.Segment, 3, 1)) AS [Value2],
CONVERT(SMALLINT, SUBSTRING(S.Segment, 2, 2)) AS [Value12],
CONVERT(SMALLINT, SUBSTRING(S.Segment, 3, 1) + SUBSTRING(S.Segment, 2, 1)) AS [Value21]
) P
Given the following input data
0x0329000414000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000
--^-----^-----^-----^-----^-----^-----^-----^-----^-----^-----^-----^-----^-----^-----
The following results are extracted:
Offset
Segment
Key
Value1
Value2
Value12
Value21
0
0x032900
3
41
0
10496
41
3
0x041400
4
20
0
5120
20
6
0x0B1400
11
20
0
5120
20
9
0x0C1400
12
20
0
5120
20
12
0x0D0F00
13
15
0
3840
15
15
0x177800
23
120
0
30720
120
18
0x224600
34
70
0
17920
70
21
0x467800
70
120
0
30720
120
24
0x473C00
71
60
0
15360
60
27
0x550F00
85
15
0
3840
15
30
0x000000
0
0
0
0
0
33
0x000000
0
0
0
0
0
36
0x000000
0
0
0
0
0
See this db<>fiddle.
DECLARE #YourTable table
(
Id INT PRIMARY KEY,
Val VARBINARY(50)
)
INSERT #YourTable
VALUES (1, 0x0329000414000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000),
(2, 0x0329002637000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000);
SELECT Id, Triplet
FROM #YourTable T
CROSS APPLY GENERATE_SERIES(1,DATALENGTH(T.Val),3) s
CROSS APPLY (VALUES (SUBSTRING(T.Val, s.value, 3))) V(Triplet)
WHERE Triplet BETWEEN 0x263700 AND 0x2637FF
This works only with '22 sql server because of 'generate_series'
DECLARE #YourTable table
(
Id INT PRIMARY KEY,
Val VARBINARY(50)
)
INSERT #YourTable
VALUES (1, 0x0329000414000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000),
(2, 0x0329002637000B14000C14000D0F00177800224600467800473C00550F00000000000000000000000000);
SELECT Id, Triplet
FROM #YourTable T
JOIN (VALUES (1),(4),(7),(10),(13),(16),(19),(22),(25),(28),(31),(34),(37),(40),(43),(46),(49)) Nums(Num) ON Num <= DATALENGTH(T.Val)
CROSS APPLY (VALUES (SUBSTRING(T.Val, Num, 3))) V(Triplet)
WHERE Triplet BETWEEN 0x263700 AND 0x2637FF
This one works on older versions without "generate_series"
The credit is to #Martin Smith from stackexchange
https://dba.stackexchange.com/questions/323235/varbinary-pattern-search

SQL Server : measuring real-time efficiency by operator

I've been working on some SQL code to measure efficiency in real-time for some production data. Here's a quick background:
Operators will enter in data for specific sub assemblies. This data looks something like this:
ID PO W/S Status Operator TotalTime Date
60129515_2000_6_S025 107294 S025 Completed A 38 05/08/2020
60129515_2000_7_S025 107294 S025 Completed A 46 05/08/2020
60129515_2000_8_S025 107294 S025 Completed A 55 05/08/2020
60129515_2025_6_S020 107295 S020 Completed B 58 05/08/2020
60129515_2025_7_S020 107295 S020 Completed B 47 05/08/2020
60129515_2025_8_S020 107295 S020 Completed B 45 05/08/2020
60129515_2000_1_S090 107294 S090 Completed C 33 05/08/2020
60129515_2000_2_S090 107294 S090 Completed C 34 05/08/2020
60129515_2000_3_S090 107294 S090 Completed C 21 05/08/2020
The relevant columns are the Operator, TotalTime and Date (note that the date is stored as varchar(50) because it plays nicer with Microsoft PowerApps that way).
What I need to do is:
Aggregate the sum of "TotalTime" grouped by Operator
Calculate the time elapsed based on a condition:
If between 7AM and 4PM, calculate the time elapsed since 7AM of the current day
If after 4PM, return the total time between 7AM and 4PM of the current day
Divide the SUM(TotalTime) by the TimeElapsed (AKA the first list item / second list item) in order to get a rough estimate of labor hours worked vs. hours passed in the day.
This calculation would change every time the query was ran. This will allow the Microsoft PowerApp that is pulling this query to refresh the efficiency measure in real time. I've taken a stab at it already - see below:
SELECT
md.Operator,
CASE
WHEN DATEADD(HOUR, -5, GETUTCDATE()) > CONVERT(DATETIME, CONVERT(DATE, DATEADD(HOUR, -5, GETUTCDATE()))) + '7:00' AND GETDATE() < CONVERT(DATETIME, CONVERT(DATE, DATEADD(HOUR, -5, GETUTCDATE()))) + '15:45'
THEN (SUM(isNull(md.TotalTime, 0)) + SUM(isNull(md.DelTime, 0))) * 1.0 / DATEDIFF(MINUTE, CONVERT(DATETIME, CONVERT(DATE, DATEADD(HOUR, -5, GETUTCDATE()))) + '7:00' , DATEADD(HOUR, -5, GETUTCDATE())) * 100.0
ELSE (SUM(isNull(md.TotalTime, 0)) + SUM(isNull(md.DelTime, 0))) / 420 * 100.0
END AS OpEfficiency
FROM
[Master Data] AS md
WHERE
md.[Date] = CONVERT(varchar(50), DATEADD(HOUR, -5, GETUTCDATE()), 101)
GROUP BY
md.Operator
Note: the DelTime is a different column regarding delay times. I am also converting back from UTC time to avoid any time zone issues when transferring to PowerApps.
However, this is horribly inefficient. I am assuming it is because the Date needs to be converted to datetime every single time. Would it work better if I had a calculated column that already had the date converted? Or is there a better way to calculate time elapsed since a certain time?
Thanks in advance.
There are a few things you can do to increase efficiency considerably. First, you want to make sure SQL can do a simple comparison when selecting rows, so you'll start by calculating a string to match your date on since your [Date] field is a string not a date.
Second, calculate the minutes in your shift (either 540 for a full shift or scaled down to 0 at 7 AM exactly) ahead of time so you aren't calculating minutes in each row.
Third, when summing for operators, use a simple sum on the minutes and calculate efficiency from that sum and your pre-calculated shift so far minutes.
One note - I'm casting the minutes-so-far as FLOAT in my example, maybe not the best type but it's clearer than other decimal types like DECIMAL(18,6) or whatever. Pick something that will show the scale you want.
My example uses a Common Table Expression to generate that date string and minutes-so-far FLOAT, that's nice because it fits in a direct query, view, function, or stored procedure, but you could DECLARE variables instead if you wanted to.
By filtering with an INNER JOIN on the [Date] string against the pre-calculated TargetDate string, I make sure the data set is pared down to the fewest records before doing any math on anything. You'll definitely want to INDEX [Date] to keep this fast as your table fills up.
All these together should give a pretty fast query, good luck
with cteNow as ( --Calculate once, up front - date as string, minutes elapsed as FLOAT (or any non-integer)
SELECT CASE WHEN 60*DATEPART(HOUR, GETUTCDATE())+DATEPART(MINUTE, GETUTCDATE()) > 60*21
--4PM in UTC-5, expressed in minutes
THEN CONVERT(float,(16-7)*60) --minutes in (4 PM-7 AM) * 60 minutes/hour
ELSE --Assume nobody is running this at 6 AM, so ELSE = between 7 and 4
CONVERT(float,60*DATEPART(HOUR, GETUTCDATE()) + DATEPART(MINUTE, GETUTCDATE()) - ((7+5)*60))
--Minutes since midnight minus minutes from midnight to 7 AM, shifted by
--UTS offset of 5 hours
END as MinutesToday --Minutes in today's shift so far
, FORMAT(DATEADD(HOUR,-5,GETUTCDATE()),'MM/dd/yyyy') as TargetDate --Date to search for
--as a string so no conversion in every row comparison. Also, index [Date] column
)
SELECT md.Operator, SUM(md.TotalTime) as TotalTime, SUM(md.TotalTime) / MinutesToday as Efficiency
FROM [Master Data] AS md INNER JOIN cteNow as N on N.TargetDate = md.[Date]
GROUP BY md.Operator, MinutesToday
BTW, you didn't make allowances for lunch or running before 7 AM, so I also ignored those. I think both could be addressed in cteNOW without adding much complexity.

Declare a local variable in named calculation expression

I would like to create a named calculation field for age, and I want to declare a local variable inside an expression like the following, but it doesn't not work :
DECLARE
#age INT;
#age=DateDiff("yyyy",DATE_NAIS,getdate());
CASE WHEN #age<=10 THEN 1
WHEN #age>10 AND #age<=20 THEN 2
WHEN #age>20 AND #age<=35 THEN 3
ELSE 4
END
Correct, you cannot DECLARE a variable in the context of an expression in a SELECT, like you are trying to do.
You have a few different options, one of which digital.aaron gave in his comments.
Another is to create it as an artificial column in a CTE, and then select from the CTE instead of the table:
WITH cte AS (SELECT *, DateDiff("yyyy",DATE_NAIS,getdate()) AS age FROM MyTable)
SELECT CASE
WHEN age<=10 THEN 1
WHEN age>10 AND age<=20 THEN 2
WHEN age>20 AND age<=35 THEN 3
ELSE 4
END AS SomeColumn
FROM cte
This question is tagged with SSAS, so I'm guessing you're doing this in a cube? The following DAX formula will work as a calculated column in your model for what was described in your post. As stated in the comments DATEDIFF alone will only return the difference between the current year and the year the person was born, which is why YEARFRAC is used to obtain the fraction of a year, then this is rounded down to the nearest integer via the INT function. For example, someone who is 50 years, 364 days old would be considered 50, not 51. Since TRUE() is given as the first argument of the SWITCH function, this will evaluate each condition until a match is found.
PersonAge:=
var Age = INT(YEARFRAC(YourTable[DATE_NAIS], TODAY()))
RETURN
SWITCH
(TRUE(),
Age < 10, 1,
AND(
Age > 10,
Age <= 20), 2,
AND(
Age > 20,
Age <= 35), 3,
4)

SQL Server : datalength conversion

I have a table dbo.files with 9 columns that include file_size and created_time and filepath.
Sample values:
file_size = 528300
created_time = 2012-06-28 09:31:17.610
I have the following query where I'm trying to show the total # of MB have been written to the filesystem 'today' by these files.
select
sum(datalength(f.file_size)) / 1048576.0 AS 'Storage Used Today"
from
dbo.files AS f
where
f.created_time >= CAST(getdate() as DATE)
and f.created_time < CAST(DATEADD(day, 1, getdate()) as DATE)
The result is '0.173525810'. Is there a way to move that decimal over to show the proper value?
Thanks
SUM(DATALENGTH(x)) tells you the size in bytes of the numeric representation.
Which isn't what you need.
For example if the datatype was integer (4 bytes) and you had three rows with none null values in the column it would evaluate to 12 irrespective of the actual numeric contents.
Just remove the function call.
sum(f.file_size) / (1024.0 * 1024)
Will work fine

SQL: Is it possible to SUM() fields of INTERVAL type?

I am trying to sum INTERVAL. E.g.
SELECT SUM(TIMESTAMP1 - TIMESTAMP2) FROM DUAL
Is it possible to write a query that would work both on Oracle and SQL Server? If so, how?
Edit: changed DATE to INTERVAL
I'm afraid you're going to be out of luck with a solution which works in both Oracle and MSSQL. Date arithmetic is something which is very different on the various flavours of DBMS.
Anyway, in Oracle we can use dates in straightforward arithmetic. And we have a function NUMTODSINTERVAL which turns a number into a DAY TO SECOND INTERVAL. So let's put them together.
Simple test data, two rows with pairs of dates rough twelve hours apart:
SQL> alter session set nls_date_format = 'dd-mon-yyyy hh24:mi:ss'
2 /
Session altered.
SQL> select * from t42
2 /
D1 D2
-------------------- --------------------
27-jul-2010 12:10:26 27-jul-2010 00:00:00
28-jul-2010 12:10:39 28-jul-2010 00:00:00
SQL>
Simple SQL query to find the sum of elapsed time:
SQL> select numtodsinterval(sum(d1-d2), 'DAY')
2 from t42
3 /
NUMTODSINTERVAL(SUM(D1-D2),'DAY')
-----------------------------------------------------
+000000001 00:21:04.999999999
SQL>
Just over a day, which is what we would expect.
"Edit: changed DATE to INTERVAL"
Working with TIMESTAMP columns is a little more labourious, but we can still work the same trick.
In the following sample. T42T is the same as T42 only the columns have TIMESTAMP rather than DATE for their datatype. The query extracts the various components of the DS INTERVAL and converts them into seconds, which are then summed and converted back into an INTERVAL:
SQL> select numtodsinterval(
2 sum(
3 extract (day from (t1-t2)) * 86400
4 + extract (hour from (t1-t2)) * 3600
5 + extract (minute from (t1-t2)) * 600
6 + extract (second from (t1-t2))
7 ), 'SECOND')
8 from t42t
9 /
NUMTODSINTERVAL(SUM(EXTRACT(DAYFROM(T1-T2))*86400+EXTRACT(HOURFROM(T1-T2))*
---------------------------------------------------------------------------
+000000001 03:21:05.000000000
SQL>
At least this result is in round seconds!
Ok, after a bit of hell, with the help of the stackoverflowers' answers I've found the solution that fits my needs.
SELECT
SUM(CAST((DATE1 + 0) - (DATE2 + 0) AS FLOAT) AS SUM_TURNAROUND
FROM MY_BEAUTIFUL_TABLE
GROUP BY YOUR_CHOSEN_COLUMN
This returns a float (which is totally fine for me) that represents days both on Oracle ant SQL Server.
The reason I added zero to both DATEs is because in my case date columns on Oracle DB are of TIMESTAMP type and on SQL Server are of DATETIME type (which is obviously weird). So adding zero to TIMESTAMP on Oracle works just like casting to date and it does not have any effect on SQL Server DATETIME type.
Thank you guys! You were really helpful.
You can't sum two datetimes. It wouldn't make sense - i.e. what does 15:00:00 plus 23:59:00 equal? Some time the next day? etc
But you can add a time increment by using a function like Dateadd() in SQL Server.
In SQL Server as long as your individual timespans are all less than 24 hours you can do something like
WITH TIMES AS
(
SELECT CAST('01:01:00' AS DATETIME) AS TimeSpan
UNION ALL
SELECT '00:02:00'
UNION ALL
SELECT '23:02:00'
UNION ALL
SELECT '17:02:00'
--UNION ALL SELECT '24:02:00' /*This line would fail!*/
),
SummedTimes As
(
SELECT cast(SUM(CAST(TimeSpan AS FLOAT)) as datetime) AS [Summed] FROM TIMES
)
SELECT
FLOOR(CAST(Summed AS FLOAT)) AS D,
DATEPART(HOUR,[Summed]) AS H,
DATEPART(MINUTE,[Summed]) AS M,
DATEPART(SECOND,[Summed]) AS S
FROM SummedTimes
Gives
D H M S
----------- ----------- ----------- -----------
1 17 7 0
If you wanted to handle timespans greater than 24 hours I think you'd need to look at CLR integration and the TimeSpan structure. Definitely not portable!
Edit: SQL Server 2008 has a DateTimeOffset datatype that might help but that doesn't allow either SUMming or being cast to float
I also do not think this is possible. Go with custom solutions that calculates the date value according to your preferences.
You can also use this:
select
EXTRACT (DAY FROM call_end_Date - call_start_Date)*86400 +
EXTRACT (HOUR FROM call_end_Date - call_start_Date)*3600 +
EXTRACT (MINUTE FROM call_end_Date - call_start_Date)*60 +
extract (second FROM call_end_Date - call_start_Date) as interval
from table;
You Can write you own aggregate function :-). Please read carefully http://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dciaggfns.htm
You must create object type and its body by template, and next aggregate function what using this object:
create or replace type Sum_Interval_Obj as object
(
-- Object for creating and support custom aggregate function
duration interval day to second, -- In this property You sum all interval
-- Object Init
static function ODCIAggregateInitialize(
actx IN OUT Sum_Interval_Obj
) return number,
-- Iterate getting values from dataset
member function ODCIAggregateIterate(
self IN OUT Sum_Interval_Obj,
ad_interval IN interval day to second
) return number,
-- Merge parallel summed data
member function ODCIAggregateMerge(
self IN OUT Sum_Interval_Obj,
ctx2 IN Sum_Interval_Obj
) return number,
-- End of query, returning summary result
member function ODCIAggregateTerminate
(
self IN Sum_Interval_Obj,
returnValue OUT interval day to second,
flags IN number
) return number
)
/
create or replace type body Sum_Interval_Obj is
-- Object Init
static function ODCIAggregateInitialize(
actx IN OUT Sum_Interval_Obj
) return number
is
begin
actx := Sum_Interval_Obj(numtodsinterval(0,'SECOND'));
return ODCIConst.Success;
end ODCIAggregateInitialize;
-- Iterate getting values from dataset
member function ODCIAggregateIterate(
self IN OUT Sum_Interval_Obj,
ad_interval IN interval day to second
) return number
is
begin
self.duration := self.duration + ad_interval;
return ODCIConst.Success;
exception
when others then
return ODCIConst.Error;
end ODCIAggregateIterate;
-- Merge parallel calculated intervals
member function ODCIAggregateMerge(
self IN OUT Sum_Interval_Obj,
ctx2 IN Sum_Interval_Obj
) return number
is
begin
self.duration := self.duration + ctx2.duration; -- Add two intervals
-- return = All Ok!
return ODCIConst.Success;
exception
when others then
return ODCIConst.Error;
end ODCIAggregateMerge;
-- End of query, returning summary result
member function ODCIAggregateTerminate(
self IN Sum_Interval_Obj,
returnValue OUT interval day to second,
flags IN number
) return number
is
begin
-- return = All Ok, too!
returnValue := self.duration;
return ODCIConst.Success;
end ODCIAggregateTerminate;
end;
/
-- You own new aggregate function:
CREATE OR REPLACE FUNCTION Sum_Interval(
a_Interval interval day to second
) RETURN interval day to second
PARALLEL_ENABLE AGGREGATE USING Sum_Interval_Obj;
/
Last, check your function:
select sum_interval(duration)
from (select numtodsinterval(1,'SECOND') as duration from dual union all
select numtodsinterval(1,'MINUTE') as duration from dual union all
select numtodsinterval(1,'HOUR') as duration from dual union all
select numtodsinterval(1,'DAY') as duration from dual);
Finally You can create SUM function, if you want.

Resources