Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 get time difference

Author  Topic 

younas02
Starting Member

28 Posts

Posted - 2012-11-27 : 02:39:38
i have a date field with type varchar. now i want to get time difference in days with today's date how can i get it?
actually i want to get rows which have day difference of more than 30 days.I have a field of lastlogin,i want to compare lastlogin with today's datetime and if the difference is more than 30 days we want to send those persons emails.

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-27 : 03:40:04
see this code once

DECLARE @fromDate datetime2 ='2012-10-27 13:05:00'

SELECT DATEDIFF(DD, @fromDate, GETDATE()) DaysDiff

SELECT DATEDIFF( hh, @fromDate, GETDATE()) HoursDiff, DATEDIFF( MI, @fromDate, GETDATE()) MinutesDiff, DATEDIFF( hh, @fromDate, GETDATE())/24 DaysDiff

SELECT convert(varchar(5),datediff(HOUR ,@fromdate,GETDATE())) + ' hour '+
convert(Varchar(5),DATEDIFF (Minute,@fromDate,GETDATE())%60) +' Minute ' +
convert(Varchar(5),DATEDIFF (SECOND ,@fromDate,GETDATE())%60) +' Second '


--
Chandu
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-11-29 : 06:33:27
Why did you use varchar datatype to store dates? What is the format of varchar dates?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -