Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Date Query - Last Full Year

Posted on 2007-03-19
1
203 Views
Last Modified: 2010-03-20
I am using the following query to bring back data from the start of the year to the end of the last month.

The date is datetime field

where  year(acd.startdatetime) = year(getdate()) and  month(acd.startdatetime) <= Month(getdate()) - 1

I now need to a query to bring back the last full years data i.e. 2006 Jan 1st - Dec 31. This will then be run on the 1st of January every year to bring back the previous years data.

Can anybody help?
0
Comment
Question by:halifaxman
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18748045
declare @startdate datetime
set @startdate = convert(datetime, cast (year(getdate())-1 as varchar(4)) + '-01-01', 120)
select ...
where  acd.startdatetime>= @startdate
and acd.startdatetime< dateadd(year, 1, @startdate)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​

856 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