The default mentioned in previous answers only works for simple cases. In more complicated cases, I use an IF clause near the beginning of the stored procedure to provide a value, if the parameter is NULL or empty and calculations are required.
I often use optional parameters in the WHERE clause, and discovered that SQL does not short circuit logic, so use a CASE statement to make sure not to try to evaluate NULL or empty dates or unique identifiers, like so:
CREATE Procedure ActivityReport
(
@FromDate varchar(50) = NULL,
@ToDate varchar(50) = NULL
)
AS
SET ARITHABORT ON
IF @ToDate IS NULL OR @ToDate = '' BEGIN
SET @ToDate = CONVERT(varchar, GETDATE(), 101)
END
SELECT ActivityDate, Details
FROM Activity
WHERE
1 = CASE
WHEN @FromDate IS NULL THEN 1
WHEN @FromDate = '' THEN 1
WHEN ActivityDate >= @FromDate AND ActivityDate < DATEADD(DD,1,@ToDate) THEN 1
ELSE 0
END