Solved

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

Posted on 2016-07-26
2
52 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
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 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 48

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
any adverse effect when modifying data type on a busy table. 4 16
get_systemdrive info from tsql? 1 18
TSQL recursive CTE challenge... 8 29
SQL trigger 5 23
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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