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
36 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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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