Jump to content
  • 0

Convert a datediff to dd:hh:mm


KAPITYAN
 Share

Question

I have a formula field in one of my tables that calculates the difference between to date/time fields. I want the results to display as dd:hh:mm.

Here is my formula as it is currently. Any suggestions would be appreciated.

CASE
WHEN [@field:TimeAssigned] Is Null
THEN ''
ELSE Datediff(minute, [@field:Time], [@field:TimeAssigned])
END

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

To achieve your desired result, please try to use this formula:


CASE
WHEN [@field:TimeAssigned] Is Null
THEN ''
ELSE
CAST((DATEDIFF(HOUR, [@field:Time], [@field:TimeAssigned]) / 24) AS VARCHAR)
+ ':' +
CAST((DATEDIFF(HOUR, [@field:Time], [@field:TimeAssigned]) % 24) AS VARCHAR)
+ ':' +
CASE WHEN DATEPART(SECOND, [@field:TimeAssigned]) >= DATEPART(SECOND, [@field:Time])
THEN CAST((DATEDIFF(MINUTE, [@field:Time], [@field:TimeAssigned]) % 60) AS VARCHAR)
ELSE
CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, [@field:TimeAssigned]), [@field:TimeAssigned]) % 60)
AS VARCHAR)
END
+ ':' + CAST((DATEDIFF(SECOND, [@field:Time], [@field:TimeAssigned]) % 60) AS VARCHAR)
END

The output will be dd:hh:mm:ss.

Link to comment
Share on other sites

  • 0

Hi @KAPITYAN,

Just to add from my previous comment above, you may try use this one as well:

CASE
WHEN [@field:TimeAssigned] IS NULL
THEN ' '
ELSE CONVERT(VARCHAR(MAX),(Datediff(day, [@field:TimeAssigned], [@field:Time]))) + CONVERT(varchar(5), DATEADD(minute, DATEDIFF(MINUTE,[@field:TimeAssigned], [@field:Time]), 0), 114)
END

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...