Solved

Date comparison - get all records from this year

Posted on 2011-09-21
3
260 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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