Solved

Date comparison - get all records from this year

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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