Solved

select full 12 months data

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing SQL Select Query result to a text file 12 48
Changing the datatype of a column from nvarchar to date 47 21
sql help 8 55
Help Required 2 33
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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