Get startedGet started for free

Multiple inputs one output

Often times you will need to pass more than one parameter to a function. Create a function that accepts @StartDateParm and @EndDateParm and returns the total ride hours for all transactions that have a StartDate within the parameter values.

This exercise is part of the course

Writing Functions and Stored Procedures in SQL Server

View Course

Exercise instructions

  • Create a function named SumRideHrsDateRange with @StartDateParm and @EndDateParm as the input parameters of datetime data type.
  • Specify the return data type to be numeric.
  • Use a select statement to sum the difference between the StartDate and EndDate of the transactions.
  • Only include transactions that have a StartDate greater than @StartDateParm and less than @EndDateParm.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

-- Create the function
___ ___ SumRideHrsDateRange (___ ___, ___ ___)
-- Specify return data type
RETURNS ___
AS
BEGIN
RETURN
-- Sum the difference between StartDate and EndDate
(___ ___(___(second, StartDate, EndDate))/3600
FROM CapitalBikeShare
-- Include only the relevant transactions
WHERE StartDate > ___ and StartDate < ___)
END
Edit and Run Code