What is Computed Column in SQL Server

Computed Column in SQL Server


In this article, we will see what is 'Computed Column in SQL Server.


DotNetKida: Computed Column in SQL Server

First of all What is a Computed column in SQL Server?

Computed column as the name gives an idea it's related to something calculation/ computation, Yes, computed columns are similar to a column in Excel with an applied formula that calculates the value automatically of the given query or the columns.
Computed columns are columns with some user-defined expression or with some formula to calculate the corresponding columns with operators or with some query.

Let's take an example:

Creating a table named 'CalculationTable' with a computed column named 'Age'





Create table CalculationTable
       (
              ID INT IDENTITY(1,1) PRIMARY KEY,
              NAME NVARCHAR(50) null,
              DATEOFBIRTH DATE null,
              AGE AS (DATEDIFF(YEAR,DATEOFBIRTH,GETDATE()))
       )





 You can also use the SSMS user interface to define that column (formula), Let's see how: 


Using SQL Server Management Studio


Now let's insert some data into the table:




Data Calculated by ComputedColumn


In the above example, AGE  is a computed column, whenever we insert the value of DATEOFBIRTH  then AGE  will be calculated automatically.

I hope this will be helpful to understand the Computed Columns in SQL Server.

for practice, you can download the scripts via google drive.

Download The Script From Here
[Download SQL Scripts via Google Drive]

Protected by Copyscape

Post a Comment

7 Comments