Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

selecting appointments for the last 12 months

Posted on 2012-03-13
5
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 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