BETWEEN ages

  • Try this ...

    DECLARE @var1 INT, @var2 INT

    SET @var1 = 25

    SET @var2 = 28

    IF (@var1 > @var2)

    BEGIN

    SET @var2 = @var1 - @var2

    SET @var1 = @var1 - @var2

    SET @var2 = @var2 + @var1

    END

    then use your BETWEEN @var1 AND @var2

  • if(@var1 > @var2) SELECT @var1 = @var2, @var2 = @var1

    should work too.

  • take it you mean this statement

    if @minage > @maxage set @minage = @maxage, @maxage = @minage --sets minage to 10, sets maxage to 1000

    the query is evalutated at run time so both @minage and @maxage are read into memory at the same time which is what allows you to swap the values around, but only if you do both sets at the same time

  • THANK YOU ALL !!, it's working this way:

    DECLARE @minage INT

    DECLARE @maxage INT

    SET @minage = @FromAge

    SET @maxage = @ToAge

    if @minage > @maxage set @minage = @maxage + @minage

    if @minage > @maxage set @maxage = @minage - @maxage

    if @minage > @maxage set @minage = @minage - @maxage

    where dbo.fAgeCalc(u.BDate,GETDATE()) BETWEEN @minage and @maxage

  • I would take a slightly different approach

    --DECLARE @FromAge -- get this from your app

    --DECLARE @ToAge -- get this from your app to i like to declare and comment out my paramaters im getting

    DECLARE @minage INT

    DECLARE @maxage INT

    IF @FromAge < @ToAge

    BEGIN

    SET @minage = @FromAge

    SET @maxage = @ToAge

    END

    ELSE

    BEGIN

    SET @minage = @ToAge

    SET @maxage = @FromAge

    END


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • THANK YOU ALL !!, it's working this way:

    DECLARE @minage INT

    DECLARE @maxage INT

    SET @minage = @FromAge

    SET @maxage = @ToAge

    if @minage > @maxage set @minage = @maxage + @minage

    if @minage > @maxage set @maxage = @minage - @maxage

    if @minage > @maxage set @minage = @minage - @maxage

    where dbo.fAgeCalc(u.BDate,GETDATE()) BETWEEN @minage and @maxage

    That's called job security! 😀


    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

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply