I want to display for the user how long ago this record was added. (Like “0 Days 5 Hours 6 Min.”) so i need to take the total number of min. and make the math. I did it in c# but now I need to do it in sql syntax, is there a easy way to do that?
UPDATE:
Thanks to @Andomar answer here I got it to work as a separate query, Now I need to add it to a big view of the calls table..
select case when days > 0 then CAST(Days as varchar(6)) + ' Days ' else +
case when hours > 1 and hours < 24 then cast(hours as varchar(6)) + ' hours'
when hours > 1 and hours < 24 then '1 hour'
else ''
end + ' ' +
case when minutes > 1 and minutes < 60 then cast(minutes as varchar(6)) + ' minutes'
when minutes = 1 then '1 min.'
else ''
end
end as TimeOpen
From (
select datediff(HH, dbo.Calls.CallDate, getdate()) as hours
, datediff(MI, dbo.Calls.CallDate,getdate()) % 60 as minutes
, datediff(D, dbo.Calls.CallDate, getdate()) as Days
from calls where Status <> 7 and Status <> 4
) as SubQuery
You can find the differences in hours like:
And the remainder of minutes:
Combined, it would look like:
To do conditional formatting, you could use a subquery:
I’ll leave adding days as an exercise for the reader 😉