Solved

date conversion works for field but not selection

Posted on 2014-03-10
4
193 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
[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
4 Comments
 
LVL 143

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 143

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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