Solved

selecting appointments for the last 12 months

Posted on 2012-03-13
5
276 Views
Last Modified: 2012-03-14
Hi

I have a table called appointments, each appointment has a date, for example 13/03/2012 and a title...

I want to write a select statement to select all appointments in the last 12 monthS only , so for example if i ran the query today i would get all appointments back to  13/03/2011....i dont want to hard code the dates as i would like to run this anyday of the year and only get the previous 12 months of appointments.

Can anyone give me the syntax please?
0
Comment
Question by:ac_davis2002
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Simone B
ID: 37717211
Select * from TBLAppointments
where AppointmentDate > dateadd(d,-365,getdate())

This will give you all appointments with the date in the last 365 days.

You could also use months:

Select * from TBLAppointments
where AppointmentDate > dateadd(m,-12,getdate())

to get all appts within the last 12 months.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37718047
or simply (no points please)
WHERE YourAppointmentDate >= DATEADD(year, -1, GETDATE())
0
 

Author Comment

by:ac_davis2002
ID: 37718938
That is almost what i need but can i get it to go back to the first of the month so for example if I run today it will give me all appointments from today 14/03/2012 back to 01/03/2011? is this possible?
0
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 37720032
Select * from TBLAppointments
where AppointmentDate > dateadd(m,-12,getdate())
or (month(appointmentdate) = month(getdate()) and year(appointmentdate = year(getdate()) - 1)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37721603
If you are concerned about performance and AppointmentDate is appropriately indexed this may prove a better approach (notice that it also takes in account the time):

DECLARE @StartDate datetime,
	@EndDate datetime

SET @EndDate = DATEADD(DAY, 1, DATEDIFF(DAY, 0, GETDATE()))
SET @StartDate = DATEADD(year, -1, DATEDIFF(DAY, 0, GETDATE()))
SET @StartDate = DATEADD(DAY, 1 - DAY(@StartDate), @StartDate)

...

WHERE AppointmentDate >= @StartDate AND AppointmentDate < @EndDate

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

831 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