?
Solved

Why is the T-SQL "DATEPART" Not Working?

Posted on 2016-07-26
2
Medium Priority
?
88 Views
Last Modified: 2016-07-26
Hello:

I'm running a simple select statement against a SQL Server 2008 R2 database, as follows:

select * from RM20101 where RMDTYPAL = 7 and DATEPART(y, RM20101.DOCDATE) < '2011'

Open in new window


The RM20101 table is a list of accounts receivable (i.e. "sales") documents.  The syntax " where RMDTYPAL = 7" means to only return a list of credit memos.  Finally, the "DATEPART" syntax is to return credit memos only where the year portion of the document's date is older than the year 2011.

But, if you look at the image that I have embedded, this statement is returning documents dated with the year 2013.

What am I doing wrong?

DOCDATE
Thank you!

John
0
Comment
Question by:John Ellis
[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
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 41730410
because y is dayofyear. you need to use yy or yyyy as shown in https://msdn.microsoft.com/en-us/library/ms174420.aspx
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41730451
I have no idea how many rows you have in that table, but for every row you are running a function
e.g. 100,000 rows = 100,000 function executions

Or, you could do this instead which leads to zero function executions

and RM20101.DOCDATE < '20110101' -- i.e. less than Jan 1 2011


"avoid using functions on data in the where clause"
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 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