SQL

Computed Column in SQL Server

  • Saturday, May 25, 2019
  • By Ravinder Sharma
  • 7 Comments

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 applied formula that calculate 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 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 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 computed column, whenever we insert the value of DATEOFBIRTH   the 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

You Might Also Like

7 comments