I have a column named Duration and for it's default value I have created a UDF that Adds two DateTime data value columns named StartDate and EndDate.
With my script below will it return the correct DateTime Value?
My Table
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [CompanyWorking].[WorkCompleted](
[ID] [int] IDENTITY(212,1) NOT NULL,
[InvestorSeekerID] [int] NOT NULL,
[Team] [nvarchar](50) NOT NULL,
[WorkingOn] [nvarchar](150) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[Percentage] [decimal](3, 2) NOT NULL,
[Duration] [datetime] NULL,
[Note] [nvarchar](150) NOT NULL,
[Created] [datetime] NOT NULL,
CONSTRAINT [PK_WorkCompleted] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] ADD CONSTRAINT [DF_WorkCompleted_Percentage] DEFAULT ((0.00)) FOR [Percentage]
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] ADD CONSTRAINT [DF_WorkCompleted_Duration] DEFAULT ([CompanyWorking].[GetDurationOfTimeSpent]()) FOR [Duration]
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] ADD CONSTRAINT [DF_WorkCompleted_Note] DEFAULT (' ') FOR [Note]
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] ADD CONSTRAINT [DF_WorkCompleted_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] WITH CHECK ADD CONSTRAINT [FK_WorkCompleted_InvestmentSeeker_ID_InvestmentSeekerID] FOREIGN KEY([InvestorSeekerID])
REFERENCES [CompanyWorking].[InvestmentSeeker] ([ID])
GO
ALTER TABLE [CompanyWorking].[WorkCompleted] CHECK CONSTRAINT [FK_WorkCompleted_InvestmentSeeker_ID_InvestmentSeekerID]
GO
UDF
CREATE FUNCTION CompanyWorking.GetDurationOfTimeSpent()
RETURNS DateTime
AS
BEGIN
DECLARE @Duration DateTime
SELECT @Duration = CAST(StartDate as DATETIME) + CAST(EndDate as DATETIME) FROM CompanyWorking.WorkCompleted
-- Return the result of the function
RETURN @Duration
END
GO
Will the UDF above return the correct date difference with the correct column value DateTime?