Solved

SQL Formula Syntax - Part 2

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

717 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