Solved

select full 12 months data

Posted on 2012-03-14
3
317 Views
Last Modified: 2012-03-14
Hi

I have been given the solution by e/e to select 12 month appointments from my appointments table

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

this is good but if i run it it gives me all appointments from today 14/03/2012 back to 14/03/2011, how can i tweak it so it gives me data back from today to 01/03/2011...i need the full previous month also?

Thanks
0
Comment
Question by:ac_davis2002
  • 2
3 Comments
 
LVL 25

Assisted Solution

by:jogos
jogos earned 333 total points
ID: 37719832
With same functions you know.  Datepart to get the day and then dataadd to subtract that number of days from the date, that is   14/03 -> not minus 14 but 13 :)
0
 
LVL 25

Assisted Solution

by:jogos
jogos earned 333 total points
ID: 37719893
But with getdate you always have a timestamp.  Like this you get rid of it

CONVERT(datetime ,
               , (CAST(YEAR(dateadd(m,-12,getdate())) AS VARCHAR(4)) +
                  CAST(MONTH(dateadd(m,-12,getdate())) AS VARCHAR(2)) + '01' )
               ,112)
0
 
LVL 11

Accepted Solution

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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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