Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

MSSQL code to find all records within 1 year

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
danmarc92 - 02 Jul 2008 16:27 GMT
Does anybody have a good code for defining records to those within 1 year. We
have a field (closingdate) and  so we need to show all records for up to 1
year after their closingdate.
Thanks
Eric Isaacs - 02 Jul 2008 16:42 GMT
WHERE tablename.closingdate >= DATEADD(d, -366, CURRENT_TIMESTAMP)

-Eric Isaacs
Plamen Ratchev - 02 Jul 2008 16:50 GMT
Here is one way:

SELECT <columns>
FROM Foo
WHERE closingdate >= DATEADD(YEAR, -1,
                              DATEADD(DAY, DATEDIFF(DAY, 0,
CURRENT_TIMESTAMP), 0));

HTH,

Plamen Ratchev
http://www.SQLStudio.com
danmarc92 - 03 Jul 2008 09:33 GMT
Thanks for your help, the querues given do work but I actually need to
restrict the records to a year from the closingdate, not a year from current
date, does anyone know how to achieve this?.
Thanks

> Here is one way:
>
[quoted text clipped - 8 lines]
> Plamen Ratchev
> http://www.SQLStudio.com 
Plamen Ratchev - 03 Jul 2008 13:14 GMT
Please provide table structure, inserts for sample data and expected
results. It is not clear from your notes what you really need.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
danmarc92 - 03 Jul 2008 13:46 GMT
select *
from tblpatientrecords
where consentdate >= appointdate
AND consentdate <= DATEADD(d, -366, closingdate)

Therefore there are effectively 3 different dates that the table stores,
startingdate, appointdate, and closingdate.
I need to show all the info in the table based on records that the
consentdate is on/after the appointdate, and they also have to be records all
records where the consentdateis within 1 year of the closingdate.

> Please provide table structure, inserts for sample data and expected
> results. It is not clear from your notes what you really need.
[quoted text clipped - 3 lines]
> Plamen Ratchev
> http://www.SQLStudio.com 
Plamen Ratchev - 03 Jul 2008 14:03 GMT
What do you get when you run the code you posted? Does it return the result
set you expect?

Perhaps you need this:

SELECT consentdate, appointdate, closingdate
FROM tblpatientrecords
WHERE consentdate >= appointdate
  AND consentdate <= DATEADD(YEAR, 1, closingdate);

If not then please post sample data and expected results.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.