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
Solved

Sql Computed Column Formula

Posted on 2008-10-28
6
1,788 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 60

Accepted Solution

by:
chapmandew earned 125 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

829 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