Solved

date conversion works for field but not selection

Posted on 2014-03-10
4
189 Views
Last Modified: 2014-03-10
If I use the code dateadd in the select statement, it works fine.   But every time I try and include it in the where statement I get no records.   I've confirmed that I have 211 records that should be pulled for April 2014.    I don't see the issue below... feel like I'm missing something!   (I put the dateadds in the select statement just to prove to myself that is the correct format.)

(SELECT        GiftFactID, SUM(InstallmentBalance) AS currentdue, InstallmentDate, DATEADD(month, 1, InstallmentDate) ,  DATEADD(month, 1, GETDATE())

FROM            FACT_GiftInstallment

WHERE DATEpart(month, InstallmentDate) = DATEADD(month, 1, GETDATE()) and
(DATEPART(year, InstallmentDate) = DATEPART(year, GETDATE()))
AND                  (InstallmentBalance > 0)

GROUP BY GiftFactID, InstallmentDate)
0
Comment
Question by:cindyfiller
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39917512
there IS an error in the query, as you don't really compare the right things.
let's see if this works better:
WHERE DATEPART(month, InstallmentDate) = datepart(month, DATEADD(month, 1, GETDATE()) ) 
and DATEPART(year, InstallmentDate) = DATEPART(year, dateadd(MONTH, 1, GETDATE()))
AND InstallmentBalance > 0 

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39917516
0
 

Author Comment

by:cindyfiller
ID: 39917528
I didn't realize I'd have to do both the datepart and the dateadd - but it definitely works.  I'll read up on the article you referenced.  Thank you so much!
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39917539
Hello,

What do you really want to do?
DATEpart(month, InstallmentDate) -> will give you month number of InstallmentDate.
DATEADD(month, 1, GETDATE()) -> will give you DATE after addinng 1 month to current date, which will be something like '04/10/2014 7:01:00 AM', and both cannot be equated.

You may want to use below code:
DatePart(month, InstallmentDate) = DatePart(month(DATEADD(month, 1, GETDATE()))
AND DATEPART(year, InstallmentDate) = DATEPART(year, DATEADD(month, 1, GETDATE()))

Open in new window

or simply:
Month(InstallmentDate) = Month(DATEADD(month, 1, GETDATE()))
AND Year (InstallmentDate) = Year (DATEADD(month, 1, GETDATE()))

Open in new window


-Harish
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

19 Experts available now in Live!

Get 1:1 Help Now