• Peter Heller (3/29/2015)


    Compliments on your article.

    If you defined the EffectiveEndDT as DiscontinuedDT your queries would not have to use Isnull. It would be sargable and make the query optimizer a little happier :-).

    Also, I always try avoid defining tables with dbo, I prefer to create a schema which in fact may be owned by dbo for a set of grouped SQL objects.

    [p]

    --

    -- Setting a default value for Maximum EffectiveEndDT

    --

    EffectiveEndDT DATETIME NOT NULL DEFAULT ('2099-12-31')

    DiscontinuedDT DATETIME NOT NULL DEFAULT ('2099-12-31')

    DECLARE @GetPriceOn DATETIME = '2013-12-31';

    --

    -- EffectiveEndDT set default value '2099-12-31'

    --

    -- sargable

    --

    SELECT ProductID, EffectiveStartDT, EffectiveEndDT, ProductPrice

    FROM dbo.ProductPrices

    WHERE @GetPriceOn >= EffectiveStartDT AND @GetPriceOn < EffectiveEndDT;

    [/p]

    Thank you for sharing this article.

    Yes you could. Often times, people don't though.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St