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
 computed columns

Author  Topic 

ngocthanh
Starting Member

2 Posts

Posted - 2006-11-06 : 23:43:05
Dear all,
Pls help me with this
I have 2 tables
Trip (TripID, Duration)
Reserve(ReserveID, TripID, StartDay, EndDate)
in which EndDate = StartDay + Trip.Duration
How can I do this?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-06 : 23:49:12
First, write a function that takes TripID as a parameter and returns
DATEADD(minute, Duration, StartDay).
Make EndDate as calculated column with
(dbo.fnFUNCTIONNAME(TripID))


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

ngocthanh
Starting Member

2 Posts

Posted - 2006-11-06 : 23:53:58
Thank Peter Larsson for your replying.

NgocThanh
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-07 : 00:07:34
I don't know the scope for the duration. If it is minutes or hours.
CREATE FUNCTION dbo.fnSomeFunctionName
(
@TripID INT
)
RETURNS DATETIME
AS
BEGIN
DECLARE @dt DATETIME

SELECT @dt = DATEADD(minute, Trip.Duration, Reserve.StartDay)
FROM Trip
INNER JOIN Reserve ON Reserve.TripID = Trip.TripID
WHERE Trip.TripID = @TripID

RETURN @dt
END

Make the EndDate as calculated column as
(dbo.fnSomeFunctionName(TripID))


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -