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?
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.
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.
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.
This:
SalePrice-Cost-(iif(isConsigned=1, Payout, 0))
Or
SalePrice-Cost-(CASE isConsigned WHEN 1 THEN Payout ELSE 0 END)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With