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
Solved

selecting appointments for the last 12 months

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encrypting SQL Server 2014 or SQL Server 2016 4 29
backup job space check 4 42
sql, case when & top 1 14 26
question about results where i dont have a match 3 20
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

809 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