Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
45 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

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

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.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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