Solved

SQL Formula Syntax - Part 2

Posted on 2004-08-04
1
1,283 Views
Last Modified: 2008-01-09
Is it possible to nest 'CASE' statements in an SQL Column formula?

I tried do that and it doesn't seem to work. I'm not sure if I have a small error somewhere or if that is just impossible.

The statement I am trying is this:

Convert(money, (CASE WHEN ([DateCompleted] IS NOT NULL) THEN
(CASE WHEN ([JobType] = 'Partner) THEN round(([TotalRewardBase] * [PartnerPercent]),2)
ELSE round(([TotalRewardBase] * [PartnerPlusPercent]),2))
ELSE 0 END),2)

Is there something else that can be done to handle more complex logic?

I also tried introducing another column called PartnerPercentUsed. It has the following formula:

CASE WHEN ([JobType] = 'Partner') THEN [PartnerPercent] ELSE [PartnerPlusPercent] END

This works fine by itself, and (theoretically) allows me to simplify the other logic as follows:

Convert(money,(CASE WHEN ([DateCompleted] IS NOT NULL) THEN round(([TotalRewardBase] * [PartnerPercentUsed]),2) ELSE 0 END),2)

But this is not accepted.

Is it possible to have formula-driven columns which depend on other formula-driven columns?

Thanks, Ted
0
Comment
Question by:tk3
1 Comment
 
LVL 12

Accepted Solution

by:
kselvia earned 500 total points
ID: 11723208
It worked fine for me. I had to add a ' after Partner and removed some extra () and added a missing END

create table tc (DateCompleted datetime, JobType varchar(10), TotalRewardBase float, PartnerPercent int, PartnerPlusPercent int)
insert tc select '1-1-04','Partner',10,10,4

alter table tc add c1 as

Convert(money,
      CASE WHEN [DateCompleted] IS NOT NULL THEN
            CASE WHEN ([JobType] = 'Partner') THEN
                  round([TotalRewardBase] * [PartnerPercent],2)
               ELSE
                  round([TotalRewardBase] * [PartnerPlusPercent],2)
            END
      ELSE
            0
      END,2)

select * from tc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now