SQL query show items from the last twelve month

I have  a table with a date field and i want to write a query where i only show the last twelve months. How?
vbnetcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
You'll want to provide a more exact definition of 'last twelve months, but in general
(air code, modify to meet your needs)
SELECT *
FROM your_table
WHERE some_date_field >= DATEADD(month, 12, GETDATE()) AND some_date_field < GETDATE()

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daniel Van Der WerkenIndependent ConsultantCommented:
You could try this too:

SELECT *
FROM your_table
WHERE your_date BETWEEN DATEADD(YEAR, -1, GETDATE()) AND GETDATE()

Open in new window


Which would return anything created between a year before now and now, more or less. I like using the BETWEEN keyword.
PortletPaulEE Topic AdvisorCommented:
I strongly caution against using BETWEEN for date ranges. Please see: "Beware of Between"

the best practice with date and time ranges is to avoid BETWEEN and to always use the form:

WHERE col >= '20120101' AND col < '20120201'
This form works with all types and all precisions, regardless of whether the time part is applicable.
Itzik Ben-Gan

I would follow the pattern suggested by Jim.

Just one small but important point though is that you state the column is of Dates, if it is of higher precision (e.g. smalldatetime, datetime or datetime2) THEN you need to take account of time too

to set time to 00:00:00 from getdate() you can use
      cast(getdate() as date)
or
     dateadd(day, datediff(day,0, getdate() ), 0)

e.g.
SELECT *
FROM your_table
WHERE some_date_field >= DATEADD(month, 12, dateadd(day, datediff(day,0, getdate() ), 0) )
AND some_date_field < GETDATE()
vbnetcoderAuthor Commented:
ty
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.