Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I tried to get recent comments stored procedure and I got it.Now I need to get recent comments title, content with last modified time log like,

I want output like this.

1 hours 25 min ago


create proc recentComments
as
begin
select title,content from userComment order by  lastModifiedOn Desc
end
Posted

1 solution

You need to use DateDiff

Here is an example to use hours and minutes:
SQL
create proc recentComments
as
begin
select title, content, 
Convert(VARCHAR(10), DateDiff(hour, GetDate(), lastModifiedOn)) + 
  ' hours ' + 
  Convert(VARCHAR(10), DateDiff(minutes, GetDate(), lastModifiedOn) - DateDiff(hour, GetDate(), lastModifiedOn) * 60) +
  ' minutes' AS timeElapsed from userComment order by  lastModifiedOn Desc
end


Here is an example to use days (when the last comment was more than 24 hours ago):
SQL
create proc recentComments
as
begin
select title, content, 
Case When DateDiff(hour, GetDate(), lastModifiedOn) > 24 Then 
Convert(VARCHAR(10), DateDiff(day, GetDate(), lastModifiedOn)) + 
  ' days' Else 
Convert(VARCHAR(10), DateDiff(hour, GetDate(), lastModifiedOn)) + 
  ' hours ' + 
  Convert(VARCHAR(10), DateDiff(minutes, GetDate(), lastModifiedOn) - DateDiff(hour, GetDate(), lastModifiedOn) * 60) +
  ' minutes' End AS timeElapsed from userComment order by  lastModifiedOn Desc
end
 
Share this answer
 
v3
Comments
Maciej Los 8-Mar-13 15:22pm    
It should works, +5!
Sandhanamurali 9-Mar-13 6:08am    
hi,
thanks for solving the stored procedure but its getting error like that Incorrect syntax near 'DateDiff'.
chaau 9-Mar-13 6:57am    
yes, there was a typo. Forgot the closing bracket. Please check again
Sandhanamurali 20-Mar-13 3:33am    
thank you dude now its working but not coming like two days ago ,one days ago can
you help me to do.
chaau 20-Mar-13 19:01pm    
I have updated the solution for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900