We help IT Professionals succeed at work.

select full 12 months data

Medium Priority
Last Modified: 2012-03-14

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?

Watch Question

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 :)
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' )
BI & Reporting Analyst
Select * from TBLAppointments
where AppointmentDate > dateadd(m,-12,getdate())
or (month(appointmentdate) = month(getdate()) and year(appointmentdate = year(getdate()) - 1)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.