?
Solved

Sql Computed Column Formula

Posted on 2008-10-28
6
Medium Priority
?
1,801 Views
Last Modified: 2010-04-21
Hi-
I wanted to create a computed column in a table.

Example: if  ( (field1+field2) - field3) >=0 then "Yes" else "No"

Can someone help me with the syntax.
Thanks
0
Comment
Question by:stacydr
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Expert Comment

by:openshac
ID: 22822676
Not sure which version of SQL you are using but try this for T-SQL
SELECT
CASE WHEN field1 + field2 - field3 >=0 THEN "Yes" ELSE "No" END

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22822680
alter table tablename
add fieldname as case when  ( (field1+field2) - field3) >=0 then 'Yes' else 'No' end
0
 
LVL 2

Author Comment

by:stacydr
ID: 22822805
Hi-Thanks
I'm using Sql 2005 -- I'm still getting a validation error...

I wrote this in the (Formula) row in the design of my table cstd_commission_quota under Computed Column Specification..

SELECT CASE when [actual_opportunities] +[actual_qualitative_score] -[total_projected_quota] >=0 THEN "Yes" ELSE "No" END
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22822827
take the SELECT off of it, and take away the double quotes

CASE when [actual_opportunities] +[actual_qualitative_score] -[total_projected_quota] >=0 THEN 'Yes' ELSE 'No' END
0
 
LVL 2

Author Closing Comment

by:stacydr
ID: 31510772
Thank you soo much!!
0
 
LVL 6

Expert Comment

by:openshac
ID: 22822865
Sorry misunderstood the question try this:

Have you thought about just doing it in the query, this will prevent de-normalised data in the table
--EXEC prQuantOptimiser_SelectSessionNumbers 15
 
 
CREATE TABLE #MyTable ( tb1 bit, tb2 bit, tbcalculatedcolumn AS CASE WHEN
tb1 = 1 then 1
WHEN tb2 = 1 then 2
END )
 
 
INSERT INTO #MyTable ( tb1, tb2 ) VALUES ( 1,1 )
INSERT INTO #MyTable ( tb1, tb2 ) VALUES ( 1,0 )
INSERT INTO #MyTable ( tb1, tb2 ) VALUES ( 0,1 )
 
SELECT * FROM #MyTable
/*
tb1 tb2 tbcalculatedcolumn
---- ---- ------------------
1 1 1
1 0 1
0 1 2
*/
 
DROP TABLE #MyTable 

Open in new window

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

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 …
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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