Solved

Sql server 2005

Posted on 2013-05-16
6
283 Views
Last Modified: 2013-05-16
How can I difference two date time and show like this "5 houres ago" or "10 minute ago" ?


suppose i have two datetime like

Start Date = 2010-01-22 15:29:55.090
End time =  2010-01-22 16:30:09.153

and I want to show "1 minute ago" format.
               
                   OR
Start Date = 2010-01-22 15:29:55.090
End time =  2010-01-24 16:30:09.153

and I want to show "2 daya ago" format.
0
Comment
Question by:dynamicweb09
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
the building blocks of this would include:

datediff(day,startDate,endDate)
datediff(hour,startDate,endDate)
datediff(minute,startDate,endDate)

What happens after '59 minutes ago'?
for example, 78 minutes, how is this to be displayed?
or, 27 hours?

I'd suggest some more definition of the requirement may be required
0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 50 total points
Comment Utility
You can do this in PHP as well, please check the following:

http://php.net/manual/en/function.strtotime.php

and in MSSQL, see the following thread

http://stackoverflow.com/questions/1873519/asp-net-and-sql-server-time-difference-in-days-hours-minutes
0
 
LVL 1

Author Comment

by:dynamicweb09
Comment Utility
I used this .but still its not working

declare @DateValue datetime
select @DateValue ='2013-05-14 15:29:55.090'

SELECT  CASE
          WHEN @DateValue-GETDATE() >= 1 THEN CAST(DATEPART(dy, @DateValue-GETDATE()) AS varchar) + ' day(s) remaining'
          WHEN @DateValue-GETDATE() >= 0 THEN CAST(DATEPART(hh, @DateValue-GETDATE()) AS varchar) + ' hour(s) remaining'
          /*Note, it's convenient to switch around for negatives*/
          WHEN GETDATE()-@DateValue <= '00:59' THEN CAST(DATEPART(mi, GETDATE()-@DateValue) AS varchar) + ' minutes(s) overdue'
          WHEN GETDATE()-@DateValue <= 1 THEN CAST(DATEPART(hh, GETDATE()-@DateValue) AS varchar) + ' hours(s) overdue'
        ELSE
          CONVERT(varchar, @DateValue-GETDATE(), 121)
        END AS Time_Scale
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 450 total points
Comment Utility
maybe this would help?
DECLARE	@START_DATE	DATETIME
DECLARE	@END_DATE	DATETIME
SET		@START_DATE = '2013-05-01 09:00:00'
SET		@END_DATE =   '2013-05-03 11:14:22'

SELECT
  CONVERT(varchar(6), DATEDIFF(day, @START_DATE, @END_DATE))
+ ' days '
+ CONVERT(varchar(6), (DATEDIFF(second, @START_DATE, @END_DATE)/ 3600) % 24)
+ ' hours '
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, @START_DATE, @END_DATE) % 3600) / 60), 2)
+ ' minutes ago'
 AS 'time ago'

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 450 total points
Comment Utility
so, when dealing with both future and past possibilities, then the datediff calculations need a reversal of dates, like this:
DECLARE	@START_DATE	DATETIME
DECLARE	@END_DATE	DATETIME
SET		@end_DATE = '2013-05-01 09:00:00'
SET		@start_DATE =   '2013-05-03 11:14:22'

SELECT
case when @start_date <= @end_date then
  CONVERT(varchar(6), DATEDIFF(day, @START_DATE, @END_DATE))
+ ' days '
+ CONVERT(varchar(6), (DATEDIFF(second, @START_DATE, @END_DATE)/ 3600) % 24)
+ ' hours '
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, @START_DATE, @END_DATE) % 3600) / 60), 2)
+ ' minutes ago'

else

  CONVERT(varchar(6), DATEDIFF(day, @END_DATE, @START_DATE))
+ ' days '
+ CONVERT(varchar(6), (DATEDIFF(second, @END_DATE, @START_DATE)/ 3600) % 24)
+ ' hours '
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, @END_DATE, @START_DATE) % 3600) / 60), 2)
+ ' minutes to go'

end

 AS 'time string'

Open in new window

see: http://sqlfiddle.com/#!3/1fa93/6823
0
 
LVL 1

Author Closing Comment

by:dynamicweb09
Comment Utility
Thanks All............
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now