Computed time column in SQL Server
Using vs2008 I have a SQL Server database attached to my web app. I want to use a computed tim开发者_开发技巧e column in the database, along the lines of :
- timenow is 1 column
- hoursleft is another column
- timeend would be another column.
I want timeend to = timenow + hoursleft.
Is it possible to do that, and if so what would the formula be that I would enter into the computed column field, and, what datatype would the columns be, timenow and timeend I would expect to be time(7) and hoursleft an int. But is that correct? THanks for any help.
- TimeNow is datatype datetime, populated with function GetUtcDate().
- HoursLeft is an int, populated as required.
- TimeEnd is a computed column of type datetime, computed as DateAdd(hh, HoursLeft, TimeNow).
You'd use this to calculate timeend from the 2 existing columns hoursleft and timenow
CAST(DATEADD(hour, hoursleft, timenow) AS time(7))
DATEADD return type is
The return data type is the data type of the date argument
So the explicit CAST probably isn't needed if timenow is time(7) as you mentioned
DATEADD(hour, hoursleft, timenow)
精彩评论