Aximili
Aximili

Reputation: 29444

Sql Server view - Select certain column depending on the value of another column

I have a table 'Products' that has these columns

ProductId
OriginalPrice
SalePrice
IsOnSale (bit)

Is it possible to create a view 'ProductsView' that has these columns

ProductId
Price

where price is either OriginalPrice or SalePrice depending on the value of IsOnSale?

Thanks in advance!

Upvotes: 0

Views: 119

Answers (1)

brian
brian

Reputation: 3695

Yes, use a case statement:

Create view CurrentPrice AS
SELECT ProductId
     , Price = CASE 
WHEN IsOnSale = 1 THEN SalePrice
ELSE OriginalPrice
END

Upvotes: 4

Related Questions