0

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 :)

Matt
  • 343
  • 2
  • 4
  • 17

3 Answers3

5

For days:

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

For hours:

CONVERT(varchar(11), DATEDIFF(minute, z.entryDate, GETDATE()) / 60) + ' Hours'
swandog
  • 741
  • 8
  • 22
1

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).

Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
0

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

KrazzyNefarious
  • 3,202
  • 3
  • 20
  • 32