Solved

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

Posted on 2016-07-26
2
32 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 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
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

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL export CSV & schedule It 9 40
Grid querry results 41 51
PL/SQL - Leading zeros 7 39
Auditing in Azure SQL Database 3 27
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 Backup & Restore 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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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

10 Experts available now in Live!

Get 1:1 Help Now