Solved

T-SQL: What in the world?  I just want to say that, if this is greater than 0, then show it for goodness sakes!

Posted on 2016-08-02
2
40 Views
Last Modified: 2016-08-02
Hello:

As you can tell from the title of this post, I'm really frustrated with something.

The first set of code below is a snippet from a query, and this code is working.  It is simply saying show me all past due customer balances greater than 180 days.

The second code snippet is really where I'm having trouble, because I'm getting syntax errors everywhere.

In that second piece of code, all I'm trying to say is if the first piece of code is > 0, then display it.  

But, no matter how many times I play with parentheses plus no matter how many years I've been in this business plus no matter how many books that I've read on T-SQL, I can never get syntax right.

Please help!  Thanks!

John

CASE WHEN DATEDIFF (dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0 
END AS [>180]

Open in new window



CASE WHEN (DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) > 0 THEN 
(CASE WHEN (DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) AS [BAD DEBT]

Open in new window

0
Comment
Question by:John Ellis
[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 Comments
 
LVL 6

Accepted Solution

by:
Manju earned 500 total points
ID: 41739823
Can you try this...

CASE WHEN (case when DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) > 0 THEN
(CASE WHEN (DATEDIFF(dd, RM20101.DOCDATE, @DATE) > 180
and RM20101.RMDTYPAL > 6
THEN RM20101.CURTRXAM * - 1  
WHEN DATEDIFF(dd, RM20101.DUEDATE, @DATE) > 180
and RM20101.RMDTYPAL < 7
THEN RM20101.CURTRXAM ELSE 0
END) else 0 end AS [BAD DEBT]
0
 

Author Comment

by:John Ellis
ID: 41739861
Thanks, manju!  This worked!

I simply needed to use two "CASE WHEN" statements at the beginning and an "extra" ELSE 0 END, at the end of the clause.

Thank you!

John
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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