Solved

Sql server 2005

Posted on 2013-05-16
6
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 49

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 49

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 49

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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