?
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
Medium Priority
?
41 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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