Question

I have a DATEDIFF result (in minutes) that i have divided by 1440 to count as days, I wish to add 'Hours' to the end of it but cant figure out how to turn the result into a string first.

DATEDIFF(mi,z.entryDate, GETDATE()) / 1440 

this returns 11, but i want it to read 11 Hours.

Ive tried to CAST AS VARCHAR but think my syntax is wrong. Please Help :)

Was it helpful?

Solution

For days:

CONVERT(varchar(11), DATEDIFF(minute, z.entryDate, GETDATE()) / 1440) + ' Days'

For hours:

CONVERT(varchar(11), DATEDIFF(minute, z.entryDate, GETDATE()) / 60) + ' Hours'

OTHER TIPS

To get hours (rounded, of course), you would divide by 60, not 1440:

SELECT CONVERT(VARCHAR(11), (DATEDIFF(MINUTE, z.entryDate, GETDATE()) / 60)) 
  + ' hours.' FROM ...;

If you want to divide by 1440, then the result is in days, not hours:

SELECT CONVERT(VARCHAR(11), (DATEDIFF(MINUTE, z.entryDate, GETDATE()) / 1440)) 
  + ' days.' FROM ...;

And please stop using things like mi shorthand (blog post for background) and converting to varchar without length (blog post for background).

I am sorry about my previous post. this should work :- cast((DATEDIFF(mi,z.entryDate, GETDATE()) / 1440) as nvarchar) + ' Hours'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top