Solved

Date comparison - get all records from this year

Posted on 2011-09-21
3
222 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
3 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
Comment Utility
use this
EventDate >= dateadd(YEAR, datediff(YEAR, 0, getdate()), 0)

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now