Click here to Skip to main content
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 18: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 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
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