SQLUSA

Microsoft SQL Server 2005

Best Practices

 

How to find the maximum of two datetime columns?

 

Compile the following function in the application database:

USE AdventureWorks;

CREATE FUNCTION fnMaxDateTime (@ColumnA datetime, @ColumnB datetime)
RETURNS datetime
AS
BEGIN
DECLARE @Stage table (DateTimeValue datetime)
DECLARE @Max datetime
INSERT @Stage SELECT @ColumnA
INSERT @Stage SELECT @ColumnB
SELECT @Max=max(DateTimeValue) from @Stage
RETURN @Max
END
GO
SELECT dbo.fnMaxDateTime('2009-02-20','2010-02-20')
GO


 

The World Leader in SQL Server Training
 
 
SQLUSA.com Home Page