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
 Date Math in SQL - how to do?

Author  Topic 

Conjurer
Starting Member

30 Posts

Posted - 2006-01-16 : 16:56:35
I have some dates stored in a field named "visit"

In a select they show as the format:
2005-07-28 10:45:00.000


So I need to write a query that will select Visits that are more than 90 days old. I thought it might be something simple like:

Select * from patientVisit where ((getdate())-Visit>90)

But that is pulling all visits not just 90 day old ones.

How do I pull the current date/time and compute the cutoff date time that would be 90 days prior for my selection Where clause?

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-16 : 17:32:04
[code]declare @custoff datetime
select @custoff = dateadd(day, -90, getdate())
select * from patientvisit where visit >= @custoff[/code]

Or

[code]select * from patientvisit where visit >= dateadd(day, -90, getdate())[/code]

Note : getdate() will return the current date & time. If you wish to retrieve the date only use
dateadd(day, 0, datediff(day, 0, getdate()))
So the select satement will be
[code]select * from patientvisit where visit >= dateadd(day, -90, dateadd(day, 0, datediff(day, 0, getdate()))
)[/code]


-----------------
'KH'

Go to Top of Page

Conjurer
Starting Member

30 Posts

Posted - 2006-01-16 : 18:33:27
BIG BIG BIG THANKS!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-17 : 00:09:08
Read more on Dates
http://www.sql-server-performance.com/fk_datetime.asp

Madhivanan

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

- Advertisement -