Solved

Date comparison - get all records from this year

Posted on 2011-09-21
3
270 Views
Last Modified: 2012-05-12
I am having trouble figuring out how to compare the datetime field EventDate and make sure my query only returns records from the current year.   So far I have the following but it's only getting greater than or equal to today:

EventDate >= GETDATE()

and EventDate looks like '2009-04-25 00:00:00.000'

Can someone help me here?  The year of the EventDate should = this year.
0
Comment
Question by:saturation
[X]
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
3 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 36577609
use this
EventDate >= dateadd(YEAR, datediff(YEAR, 0, getdate()), 0)

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36577690
If there is any possibility that EventDate can hold future data:

WHERE EventDate >= DATEADD(year, DATEDIFF(year, '2000-01-01', GETDATE()), '2000-01-01') AND
    EventDate < DATEADD(year, DATEDIFF(year, '2000-01-01', GETDATE()) + 1, '2000-01-01')

Open in new window

0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 36577853
Or you could just use YEAR(EventDate), but I would not use getdate() in your where clause necessarily.

You could create a variable @ThisYear int and SET @ThisYear = YEAR(GETDATE())
then WHERE Year(EventDate) = @ThisYear

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
In this article I will describe the Detach & Attach 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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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