Click here to Skip to main content
12,396,230 members (55,871 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL Server
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 7-Mar-13 17:50pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to use DateDiff

Here is an example to use hours and minutes:
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):
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
  Permalink  
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'.
Andrew Cherednik 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.
Andrew Cherednik 20-Mar-13 19:01pm
   
I have updated the solution for you
Sandhanamurali 21-Mar-13 3:44am
   
thank you its working now

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100