Solved

selecting appointments for the last 12 months

Posted on 2012-03-13
5
271 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now