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
 General SQL Server Forums
 New to SQL Server Programming
 Compare 2 dates and calculate number of days diff

Author  Topic 

duncant
Starting Member

18 Posts

Posted - 2010-11-09 : 05:32:37
I have a 'StartRecorded' field, which records DateTime in UK format and I'm trying to calculate that field against todays date using the 'getDate' command, but this is in U.S.A format, then calculate the 'NumberOfDays' from the 2 fields, but I'm running into a few problems:

Code:
select ID
,convert (varchar (8),StartRecordDate,1) as StartRecordedDate
,convert (varchar(8),getdate(),3) as TodaysDate
,DateDiff(day, StartRecordedDate, TodaysDate) as numberOfDays
from tbl_myTable

Errors:
Invalid column name 'StartRecordedDate'.
Invalid column name 'TodaysDate'.

I thought converting them to a readable format and comparing the 2 values would work.

Any advice?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-09 : 05:44:51
select *,DateDiff(day, StartRecordedDate, TodaysDate) as numberOfDays
from (select ID
,convert (varchar (8),StartRecordDate,1) as StartRecordedDate
,convert (varchar(8),getdate(),3) as TodaysDate
from tbl_myTable
) as t

Madhivanan

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

duncant
Starting Member

18 Posts

Posted - 2010-11-09 : 05:56:20
Nice one, many thanks for that!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-10 : 10:25:20
why should you convert them to varchar if they're datetime already? If you want to find the difference between them its best to keep them as dates itself. Also make sure you're using proper data types for your fields

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -