Home All Groups Group Topic Archive Search About
Author
2 Sep 2005 3:01 PM
js
hi,
what diff:
select * from mytable where intfieldname between 1 and 5
and
select * from mytable where intfieldname between 5 and 1?
Thanks.

Author
2 Sep 2005 3:10 PM
David Portas
X BETWEEN Y AND Z

is equivalent to:

X>=Y AND X<=Z

so your second example will never return any rows.

--
David Portas
SQL Server MVP
--
Author
2 Sep 2005 3:11 PM
Tibor Karaszi
The second will return 0 rows. The smaller value need to be the first value you list.

(SQL Server allowed both to return rows in some prior version, it was changed in, I believe, 6.0, to
adhere to the ANSI SQL standard.)

Show quote
"js" <js@some***@hotmail.com> wrote in message news:%23itV%2388rFHA.3392@TK2MSFTNGP11.phx.gbl...
> hi,
> what diff:
> select * from mytable where intfieldname between 1 and 5
> and
> select * from mytable where intfieldname between 5 and 1?
> Thanks.
>
>
Author
2 Sep 2005 3:26 PM
David Portas
The Standard changed too. SQL99 defined a SYMMETRIC / ASYMMETRIC clause
for the BETWEEN predicate. ASYMMETRIC - the old behaviour - is still
the default, but:

X BETWEEN SYMMETRIC Y AND Z

is equivalent to:

((X BETWEEN ASYMMETRIC Y AND Z)
OR (X BETWEEN ASYMMETRIC Z AND Y))

--
David Portas
SQL Server MVP
--
Author
4 Sep 2005 5:35 PM
Tibor Karaszi
That's news to me. Thanks David. :-)

Show quote
"David Portas" <REMOVE_BEFORE_REPLYING_dpor***@acm.org> wrote in message
news:1125674797.272562.256010@g47g2000cwa.googlegroups.com...
> The Standard changed too. SQL99 defined a SYMMETRIC / ASYMMETRIC clause
> for the BETWEEN predicate. ASYMMETRIC - the old behaviour - is still
> the default, but:
>
> X BETWEEN SYMMETRIC Y AND Z
>
> is equivalent to:
>
> ((X BETWEEN ASYMMETRIC Y AND Z)
> OR (X BETWEEN ASYMMETRIC Z AND Y))
>
> --
> David Portas
> SQL Server MVP
> --
>
Author
4 Sep 2005 10:51 PM
--CELKO--
For one meeting, the default behavior in the working draft SQL2, was
SYMMETRIC.  Microsoft was attending meetings and they picked it up.
Then the next meeting we switched it back because almost no products
were SYMMETRIC except MS.
Author
5 Sep 2005 6:33 AM
Tibor Karaszi
That sounds strange to me. SQL Server has been SYMMETRIC since 1.0 to some 4.21 (as I remember).
Probably a legacy from Sybase. And in 6.0 or it could have been 4.2, they changed it to ASYMMETRIC.
So I doubt that MS picked up SYMMETRIC from an ANSI meeting, as this was the behavior since a lot
earlier. And prior to 6.0, Sybase made those calls, not MS.

Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1125874296.990565.129420@g44g2000cwa.googlegroups.com...
> For one meeting, the default behavior in the working draft SQL2, was
> SYMMETRIC.  Microsoft was attending meetings and they picked it up.
> Then the next meeting we switched it back because almost no products
> were SYMMETRIC except MS.
>
Author
2 Sep 2005 3:14 PM
Alejandro Mesa
can you see it?

select * from t1 where c1 >= 1 and c1 <= 5
select * from t1 where c1 >= 5 and c1 <= 1


AMB

Show quote
"js" wrote:

> hi,
> what diff:
> select * from mytable where intfieldname between 1 and 5
> and
> select * from mytable where intfieldname between 5 and 1?
> Thanks.
>
>
>
Author
2 Sep 2005 3:33 PM
Aaron Bertrand [SQL Server MVP]
Order matters.  The left side must be smaller.

My typical suggestion is to avoid between for this and other reasons...

http://www.aspfaq.com/2280



Show quote
"js" <js@some***@hotmail.com> wrote in message
news:%23itV%2388rFHA.3392@TK2MSFTNGP11.phx.gbl...
> hi,
> what diff:
> select * from mytable where intfieldname between 1 and 5
> and
> select * from mytable where intfieldname between 5 and 1?
> Thanks.
>
>
Author
2 Sep 2005 3:59 PM
David Portas
"When you are using other clauses in your query, you also have to
remember to wrap the BETWEEN clause in brackets so that its AND isn't
confused with other ANDs in the WHERE clause."

Just to be clear about this point. Parenthesizing the BETWEEN predicate
probably aids readability but has no effect on the order of evaluation.
That's because the AND keyword in the BETWEEN predicate has nothing to
do with the Boolean operator of the same name. "x BETWEEN y AND z" is
evaluated as a single expression. I mention this detail because I've
found people get similarly confused over the predicate "IS NOT NULL",
which is three keywords but a single expression.

--
David Portas
SQL Server MVP
--
Author
2 Sep 2005 4:08 PM
Aaron Bertrand [SQL Server MVP]
Yes, the point about evaluation was meant to apply only to the point about ,
not that x = 5 or x between 7 and 12 or x = 4 would be evaludated in some
weird way.  The bullet itself was added merely to address readability
concerns, but it does bring up the notion of more complex queries.  I'm not
sure how to word it better to turn off the alarm bells that caused your
reply, however...



Show quote
"David Portas" <REMOVE_BEFORE_REPLYING_dpor***@acm.org> wrote in message
news:1125676773.780264.262550@g49g2000cwa.googlegroups.com...
>> http://www.aspfaq.com/2280
> Quote:
>
> "When you are using other clauses in your query, you also have to
> remember to wrap the BETWEEN clause in brackets so that its AND isn't
> confused with other ANDs in the WHERE clause."
>
> Just to be clear about this point. Parenthesizing the BETWEEN predicate
> probably aids readability but has no effect on the order of evaluation.
> That's because the AND keyword in the BETWEEN predicate has nothing to
> do with the Boolean operator of the same name. "x BETWEEN y AND z" is
> evaluated as a single expression. I mention this detail because I've
> found people get similarly confused over the predicate "IS NOT NULL",
> which is three keywords but a single expression.
>
> --
> David Portas
> SQL Server MVP
> --
>
Author
2 Sep 2005 4:27 PM
js
Thanks all for the help.

AddThis Social Bookmark Button