[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Formula Syntax - Part 2

Posted on 2004-08-04
1
Medium Priority
?
1,291 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

649 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