?
Solved

SQL Formula Syntax - Part 2

Posted on 2004-08-04
1
Medium Priority
?
1,290 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
[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
1 Comment
 
LVL 12

Accepted Solution

by:
kselvia earned 2000 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

771 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