Q

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.

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:

Figure 1


Do you have comments on this Ask the Expert Q&A? Let us know.
This was last published in May 2005

Dig Deeper on Microsoft SQL Server Performance Monitoring and Tuning

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close