![]() |
|
|
By: Atif Shehzad | Read Comments (6) | Print Atif is a passionate SQL Server DBA, technical reviewer and article author. Related Tips: More |
|
Solution
For such scenarios where calculated values are required or values are generated through manipulation on other columns, we have a powerful feature provided in SQL Server. This feature is "Computed Columns".
A computed column is computed from an expression that can use another column or columns in the same table. Functions, variables, constants, non computed column names or any combination of all these may be used along with operators to create a computed column. In this tip we will go through an example of implementing a computed column.
I have tested the scripts on SQL Server 2005 and SQL Server 2008. In the following script we will create a table called CCtest in the AdventureWorks database with three columns [empNumb], [DOBirth] , [DORetirement].
We are required to have the "Date of Retirement" for each employee as (DOBirth + 60 years - 1 day). Instead of calculating it each time in the report or updating the column [DORetirement] each time through a trigger when [DOBirth] is updated, we have a better approach here to create [DORetirement] as a computed column. Since this rule could change at anytime we are implementing it as a computed column instead of a hard coded value.
| Script # 1: Create a table with computed column |
USE [AdventureWorks] GO -- Create Table with computed column CREATE TABLE [dbo].[CCtest] ( [empNumb] [int] NULL, [DOBirth] [datetime] NULL, [DORetirement] AS (dateadd(year,(60),[DOBirth])-(1)) PERSISTED ) GO |
The same may be done through SSMS. Create new table by right clicking on the Tables folder in the AdventureWorks database.

You will be provided a design view for the new table in SSMS. Provide specifications for the calculated column as shown below.

Now we have our table CCtest with a computed column. Similarly we can add a computed column to any existing table using the "ALTER TABLE" command or opening the table in design view using SSMS and making the changes.
Let's insert some data and run a query to test the functionality of the computed column.
|
Script # 2: Insert data in table |
USE AdventureWorks GO INSERT INTO CCTest (empNumb, DOBirth) SELECT 30 ,'1985-12-13' UNION ALL SELECT 25 ,'1980-11-18' UNION ALL SELECT 21 ,'1978-01-19' UNION ALL SELECT 7 ,'1985-12-13' UNION ALL SELECT 5 ,'1975-07-23' GO SELECT * FROM dbo.CCTest GO |
Here we can see our computed column:

To verify that the computed column will be updated for any updates, we will update [DOBirth] for [empNumb] 25.
|
Script # 3: Update DOBirth of empNumb 25 |
USE AdventureWorks GO UPDATE CCtest SET DOBirth = '1960-03-25' WHERE empnumb = 25 GO SELECT * FROM dbo.CCTest WHERE Empnumb = 25 GO |
Here we can see our computed column has been updated.

Persisted
You may have noticed that we also used the property "Persisted" for our computed column. This property for computed columns has been introduced in SQL Server 2005 and onwards. It is important for any computed column, because many additional features depend on it. To be able to make a computed column as Persisted it has to be deterministic.
Here are a few rules:
Nullability
Nullibility for a computed column value will be determined by the database engine itself. The result of a non-nullable referenced column may be NULL in certain conditions to avoid possible overflows or underflows. You can provide an alternate value for NULL using the ISNULL(check_expression, constant), if required.
Some Limitations
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Thursday, March 04, 2010 - 1:34:42 PM - dlsampson | Read The Tip |
|
Excellent Tip! Worked perfectly. Now, I need some help expanding on this. I have a table for Approvals in which I calculate expiration dates for the approval. What I want to do is calculate different expiration dates based upon approval type stored in a single column ApprovalType. Conditional Approval expires after 1-year Full Approval expires after 5-years. Using the Computed Columns in SQL Server tip, how can I calculate dates based upon differing values?
Thanks in advance for your help with this. David |
|
| Thursday, March 04, 2010 - 3:46:22 PM - dlsampson | Read The Tip |
|
Wahoo! I figured it out! You can use CASE functions within the Computed Columns. Here's what worked for me: (case [ApprovalType] when (2) then dateadd(year,(5),[ApprovalDate]) else dateadd(year,(1),[ApprovalDate]) end) So, now, all my Conditional Approvals have a 1-year expiry date and all my Full Approvals have a 5-year date. David |
|
| Friday, March 05, 2010 - 12:15:07 AM - @tif | Read The Tip |
|
Case statement as you used, is very suiatble for this task. Thanks |
|
| Wednesday, February 01, 2012 - 3:02:49 PM - Chris Cody | Read The Tip |
| One thing to be aware of, if your computed column derives its value from a function, and you want to change how the function works, you may have to rebuild your table containing the computed column. This means that you should really really like your function before you use it in a computed column. | |
| Wednesday, February 01, 2012 - 11:52:44 PM - Atif | Read The Tip |
|
Chiris, Thanks for pointing out this important aspect. I would cover this process and associated concepts in a seperate article. |
|
| Wednesday, May 09, 2012 - 4:40:08 PM - Jim | Read The Tip |
|
I needed to add
for all my stored procs editing the table with a computed column... (not in the proc, rather during creation) to avoid errors like this: [UPDATE failed because the following SET options have incorrect settings: QUOTED_IDENTIFIER] SET ANSI_NULLS ON ... |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |