Solved

SQL Formula Syntax - Part 2

Posted on 2004-08-04
1
1,282 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

13 Experts available now in Live!

Get 1:1 Help Now