Solved

select full 12 months data

Posted on 2012-03-14
3
316 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

861 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