SQL Server - How to calculate a difference datetime

Asked By Daniel on 07-Oct-11 01:30 PM
Hi!I have created the following table

CREATE TABLE dbo.Federation_Coop



Name_Fed varchar(50) NOT NULL,

Date_demand datetime NOT NULL,

Date_diff  datetime NOT NULL
What I want is just  the field Date_diff to be the difference between the field Date_demand and the current date(or system date)  at the time of creating of the table.
 to be specifik let me give you an exemple bellow of a field that I ve created and  pass the value directly in creation of a table and field :

 TotalDue AS (isnull((SubTotal+TaxAmt)+Freight,(0))),


Reena Jain replied to Daniel on 08-Oct-11 02:23 AM

Well, You can not assign value at the time of creation the table. You can assign sql function for default value. But to apply custom formula you need to give at the time of insertion of values in table. because value would be inserted using insert command in sql server.

Hope this will help you
Daniel replied to Reena Jain on 08-Oct-11 05:51 AM
Please reena,can you explain to me what the meaning of the line in red of table below of AdventureWorks database  :

CREATE TABLE SalesLT.SalesOrderDetail(
    SalesOrderID int NOT NULL,
    SalesOrderDetailID int IDENTITY(1,1) NOT NULL,
    OrderQty smallint NOT NULL,
    ProductID int NOT NULL,
    UnitPrice money NOT NULL,
    UnitPriceDiscount money NOT NULL,
    LineTotal  AS (isnull((UnitPrice*((1.0)-UnitPriceDiscount))*OrderQty,(0.0))),
    rowguid uniqueidentifier ROWGUIDCOL  NOT NULL,
    ModifiedDate datetime NOT NULL