Skip to content
Advertisement

Conditionally add a column with a default value to an existing table in SQL Server

I want to add a new column in existing SQL table with default values depending upon some cases/conditions.

I want to know if this is possible. If yes how? if not why?

Alternative :

One option is to create the column and then update but approach is out of the question here.

Let’s consider I have the following table

Desired output is this :

I have studied this post but this is only for “Adding column default values”

EDIT : Here is the SQL script with schema and data for the above table for users who wants to test.

Advertisement

Answer

You may try this. There are basically 2 approach to achieve this.

Adding Column in table schema

As I understand you want to add a column in table schema as given link shared by you. You may use case statement to add column for calculated values.

EDIT : Correcting Syntax Error

Or

Create view

You may create a view for same and use it wherever you needed.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement