I have built a select statement to pick the date the participant was first recorded in the database. However I want to create a function out of this select statement to use it. I am fairly new to SQL and have never built a function before. My select statement looks like this:
Select DATEDIFF(day, (select min(startdatetime)
from GamePlay
), enddatetime)
from GamePlay
where ParticipantID = '200'
My attempted function looks like this:
CREATE FUNCTION daysPlayed (@ParticipantID int)
RETURNS DateTime
AS
BEGIN
Return DATEDIFF(day, (select min(startdatetime)
from GamePlay
), enddatetime)
from GamePlay
where ParticipantID = @ParticipantID
END
GO
DATEDIFF does not return a datetime. It returns an int.
Your function might look like this:
but i don’t think it will do what you want it to do.
Also, please note that using functions in selects over a large number of rows is a guarantee performance hit and you might not be able to use some of the indexes you set up in your tables.