how to use ?
Hello.
you may use computed column
Think an example :
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.
Step 1: Create a table with computed columnCREATE TABLE [dbo].[CCtest]
( [empNumb] [int] NULL,
[DOBirth] [datetime] NULL,
[DORetirement] AS (dateadd(year,(60),[DOBirth])-(1)) PERSISTED -- Create Table with computed column
) GO
Step 2:
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 GOResult :
30 1985-12-13 00:00:00.000 2045-12-12 00:00:00.000
25 1980-11-18 00:00:00.000 2040-11-17 00:00:00.000
21 1978-01-19 00:00:00.000 2038-01-18 00:00:00.000
7 1985-12-13 00:00:00.000 2045-12-12 00:00:00.000
5 1975-07-23 00:00:00.000 2035-07-22 00:00:00.000