Cogniza | Business-Intelligence Specialists

Jan/07

26

SQL Server: MaximumDate() Function

A SQL Server, scalar-value function that returns the greater of two dates.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

/*----------------------------------------------------------------------------------------------------
Author:        Craig Buchanan
Date:        2/6/2006
Description:    Returns the greater of two dates
Parameters:    @Date1 - datetime to be compared
@Date2 - datetime to be compared
----------------------------------------------------------------------------------------------------*/
CREATE FUNCTION [dbo].[MaximumDate] (
@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

· ·

No comments yet.

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me