Home Forums SQL Server 2008 T-SQL (SS2K8) Is there a Coalesce LIke function that returns the lowest/highest value of a set of params passed to it RE: Is there a Coalesce LIke function that returns the lowest/highest value of a set of params passed to it

  • And V2 is even better

    CREATE FUNCTION [dbo].[HiLoDate2]

    (

    @date1 datetime,

    @date2 datetime,

    @date3 datetime,

    @date4 datetime

    )

    RETURNS TABLE

    AS

    RETURN

    (

    with cte (max1,max2, min1,min2 ) as

    (select case when @date1 >= @date2 then @date1 else @date2 end

    ,case when @date3 >= @date4 then @date3 else @date4 end

    ,case when @date1 <= @date2 then @date1 else @date2 end

    ,case when @date3 <= @date4 then @date3 else @date4 end

    )

    select case when min1 <= min2 then min1 else min2 end as LoDate

    ,case when max1 >= max2 then max1 else max2 end as HiDate

    from cte

    )

    GO

    /* TEST -- compare executions plans

    select *

    from dbo.HiLoDate('3/1/2012','3/15/2011','2/1/2012','11/1/2011')

    select *

    from dbo.HiLoDate2('3/1/2012','3/15/2011','2/1/2012','11/1/2011')

    */

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills