Calculate day gap and create a new column for the results

I would like to calculate the day gap and create a new column for the results. Here is my code:
SELECT
DATEDIFF(day,FromDOS,ThruDOS) as LengthOfStay FROM tbl_Master 

I want my new column to be added to the table I have queried. I have tried several coding options and scoured my SQL books to no avail. I know this must be simple. That is what makes it so frustrating.

    Requires Free Membership to View

Here is some simple code for the table that has LengthofStay as a computed column to properly calculate the number of days between the two date columns:
/****** Object:  Table [dbo].[tbl_Master]    Script Date: 
5/9/2005 9:45:17 AM ******/
CREATE TABLE [dbo].[tbl_Master] (
 [LOSID] [int] NOT NULL ,
 [FromDOS] [datetime] NOT NULL ,
 [ThruDOS] [datetime] NOT NULL ,
 [LengthofStay] AS (datediff(day,[FromDos],[ThruDos])) 
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tbl_Master] WITH NOCHECK ADD 
 CONSTRAINT [PK_tbl_Master] PRIMARY KEY  CLUSTERED 
 (
  [LOSID]
 )  ON [PRIMARY] 
GO

Here is a screen shot with the sample data:


Do you have comments on this Ask the Expert Q&A? Let us know.

This was first published in May 2005

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.