2014-08-19

TSQL - Function that returns the greater of two given dates

/*

       -- Execution
       select dbo.getGreaterDate( '1/31/2012', '8/30/2012' );
       select dbo.getGreaterDate( '1/31/2012', null );
       select dbo.getGreaterDate( null, '8/30/2012' );
       select dbo.getGreaterDate( null, null );



*/
CREATE function [dbo].[getGreaterDate](
       @date1 date
       , @date2 date
)
returns date
as
begin
       return(
              case when isnull(@date1,'1/1/1900') >= isnull(@date2,'1/1/1900') then @date1 else @date2 end
       );
end
;






No comments:

Post a Comment