Solved

Sql Computed Column Formula

Posted on 2008-10-28
6
1,793 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 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