?
Solved

Date comparison - get all records from this year

Posted on 2011-09-21
3
Medium Priority
?
275 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 2000 total points
ID: 36577609
use this
EventDate >= dateadd(YEAR, datediff(YEAR, 0, getdate()), 0)

Open in new window

0
 
LVL 93

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 25

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

800 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