Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement in SQL computed Column Specification

I'm trying to built in if statement in computed column What I'm trying to accomplish should look like this but this is more pseudo code: SalePrice-Cost-(if(isConsigned=1 Then Payout else 0))

How do I this and can I use if statement inside computed column or it should be something else?

like image 352
TheAccountant Avatar asked Dec 30 '16 23:12

TheAccountant


People also ask

What is computed column specification in SQL Server?

A computed column in SQL Server is a virtual column that computes its values from an expression. We can use a constant value, function, value derived from other columns, non-computed column name, or their combinations. SQL Server does not store these virtual columns physically, so it does not require any storage.

How can you use a computed column in an SQL query?

Go to your database, right click on tables, select “New Table” option. Create all columns that you require and to mark any column as computed, select that column and go to column Properties window and write your formula for computed column.

Can we use computed column in where clause?

Computed columns just like an ordinary table column can be used in a select list, can take place in WHERE clauses and in ORDER BY clauses. But computed columns cannot be the directly updated or values can not be inserted into computed columns using INSERT or UPDATE statements.


1 Answers

This:

SalePrice-Cost-(iif(isConsigned=1, Payout, 0))

Or

SalePrice-Cost-(CASE isConsigned WHEN 1 THEN Payout ELSE 0 END)
like image 75
Alan Burstein Avatar answered Nov 14 '22 20:11

Alan Burstein