Solved

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

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

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

939 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

15 Experts available now in Live!

Get 1:1 Help Now