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.
/****** 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.
Related Q&A from Jeremy Kadlec, Contributor
Adding non-clustered indexes to SQL Server columns and tables frequently queried is possible. Learn how to add non-clustered indexes to SQL Server ...continue reading
Learn how to view a query execution plan in SQL Server with SQL Server Management Studio or Query Analyzer. Review and decrease query plan sub tree ...continue reading
Want to change the collation to SQL Server 2005 standards after upgrading databases from SQL Server 2000? Learn the best approach involving DTS and ...continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.