Skip to content

Instantly share code, notes, and snippets.

@craibuc
Last active December 30, 2015 10:28
Show Gist options
  • Select an option

  • Save craibuc/7815672 to your computer and use it in GitHub Desktop.

Select an option

Save craibuc/7815672 to your computer and use it in GitHub Desktop.
A SQL Server, scalar-value function that returns the lesser of two dates.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/*----------------------------------------------------------------------------------------------------
Author: Craig Buchanan
Date: 2/6/2006
Description: Returns the lesser of two dates
Parameters:
@Date1 - datetime to be compared
@Date2 - datetime to be compared
----------------------------------------------------------------------------------------------------*/
CREATE FUNCTION [dbo].[MinimumDate] (
@Date1 datetime,
@Date2 datetime
)
RETURNS datetime AS
BEGIN
RETURN CASE
WHEN @Date1 IS NULL AND @Date2 IS NULL THEN NULL
WHEN @Date1 IS NULL THEN @Date2
WHEN @Date2 IS NULL THEN @Date1
WHEN @Date1 < @Date2 THEN @Date1
WHEN @Date2 < @Date1 THEN @Date2
WHEN @Date2 = @Date1 THEN @Date2
ELSE NULL
END
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment