Click here to Skip to main content
11,644,731 members (65,214 online)
Rate this: bad
good
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 at 8-Mar-13 15:22pm
   
It should works, +5!
Sandhanamurali at 9-Mar-13 6:08am
   
hi,
thanks for solving the stored procedure but its getting error like that Incorrect syntax near 'DateDiff'.
Andrew Cherednik at 9-Mar-13 6:57am
   
yes, there was a typo. Forgot the closing bracket. Please check again
Sandhanamurali at 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 at 20-Mar-13 19:01pm
   
I have updated the solution for you
Sandhanamurali at 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
0 OriginalGriff 680
1 jyo.net 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 404
4 CPallini 386
0 OriginalGriff 1,020
1 Sergey Alexandrovich Kryukov 673
2 DamithSL 656
3 Afzaal Ahmad Zeeshan 652
4 CPallini 555


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2015
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