Solved

Sql server 2005

Posted on 2013-05-16
6
288 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
ID: 39170829
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
ID: 39170832
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
ID: 39170853
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 450 total points
ID: 39170866
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
ID: 39170879
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
ID: 39170906
Thanks All............
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
Viewers will learn how the fundamental information of how to create a table.

828 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