Solved

select full 12 months data

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now