NoughT
NoughT

Reputation: 673

How i write a formular for get value for a column in sql server 2012?

I have a table with 2 column named unit_Perchus_Price and unit_sale_price. then I want to add a column named profit_margin and it values can get from this formular.

Profit Margin = (1 - (UnitPurchasePrice / UnitSalePrice)) * 100

can I do that in sql server 2012 ??

Upvotes: 0

Views: 29

Answers (1)

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

Yes, this is called a computed column

alter table t
add ProfitMargin as (1 - (UnitPurchasePrice / UnitSalePrice)) * 100

Some basic remarks on computed columns :

It's rather simple when you use value from the same table. If you need values from other tables, you'll need a function (and impact on performances can be important).

And you can't use a computed column in the computation of another computed column.

Upvotes: 3

Related Questions