[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

date conversion works for field but not selection

Posted on 2014-03-10
4
Medium Priority
?
198 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 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