Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculated Column

Author  Topic 

elyanivson
Starting Member

6 Posts

Posted - 2011-09-16 : 03:10:56
Hi,
I want to make a calculated column with the following details.
I have 2 Column:
1.Check_In_date(DateTime)
2.Check_Out_Date(DateTime)

I want a column that shows the Number Of nights.
Actually, Check_Out_date-Check_in_date.

I know I should use the Computed Column Specification, but I don't know the Syntax or the Function I should use.
Thank you very much in advance!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-09-16 : 03:15:55
[code]
alter table <table name> add number_of_nights as datediff(day, Check_In_date, Check_Out_Date)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2011-09-16 : 03:16:45
This will do.

DATEDIFF(DD,Check_In_date,Check_Out_date)

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-16 : 05:11:15
"I know I should use the Computed Column Specification, but I don't know the Syntax or the Function I should use."

I prefer to use a VIEW. I've had problems with computed columns in the past when I came to do something like adding an indexed view or instead-of trigger on a table. You may not be doing that, or planning to do that, but its a PITA if you want to do it and then find you can't!

CREATE VIEW dbo.MyTableName_VIEW
AS
SELECT [V_MyPK] = MyPK,
[V_number_of_nights] = datediff(day, Check_In_date, Check_Out_Date)
FROM dbo.MyTableName

and then in your queries:

SELECT Check_In_date, Check_Out_Date, V_number_of_nights #
FROM dbo.MyTableName
JOIN MyTableName_VIEW
ON V_MyPK = MyPK
Go to Top of Page

elyanivson
Starting Member

6 Posts

Posted - 2011-09-16 : 12:46:35
Thank you very much guys!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-09-16 : 15:20:44
I prefer that the front end handles presentation issues

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -