If we use cast or convert in where clauses, will SQL Server 2000 still
treat that as a possible SARG (search argument) with the possibility of
using indexes, or do we lose that ability? I'm thinking in particular
of dates and numbers from text.
Thanks.No, it will not be a SARG. Make sure you use proper datatype for the columns
and you will limit the
need for this. As for datetime searches, I have some tips in
http://www.karaszi.com/SQLServer/info_datetime.asp.
SQL Server does some tricks when you have an (implicit) cast between datatyp
es in the same group
(like between the integer class datatypes). For example (Northwind database)
:
SELECT * FROM [Order Details]
WHERE OrderID = 23
The constant 23 will be treated as a tinyint. OrderID is an int. Since int h
as higher datatype
precedence (see BOL for list), the tinyint will be converted to int. You can
see that in the
execution plan. I.e., the conversion is at the constant side so this is stil
l a SARG. But consider
below:
CREATE INDEX x ON [Order Details](Quantity)
SELECT * FROM [Order Details]
WHERE Quantity = 34556647
The constant 34556647 is an int where Quantity is smallint. So now Quantity
need to be converted, so
you have a CAST on the column side. Look at the execution plan and you will
see those tricks taken
by SQL server in order to use an index in this particular case.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"OrchidPop" <cristoff@.charter.net> wrote in message
news:1124266316.795238.311570@.f14g2000cwb.googlegroups.com...
> If we use cast or convert in where clauses, will SQL Server 2000 still
> treat that as a possible SARG (search argument) with the possibility of
> using indexes, or do we lose that ability? I'm thinking in particular
> of dates and numbers from text.
> Thanks.
>|||Thanks for the reply. I had hoped cast/convert were not considered true
functions to block SARGs.
Actually, I need to check with the original developer of our
application to find out why convert is used all over the
place,particularly for dates.
Thanks!|||> Actually, I need to check with the original developer of our
> application to find out why convert is used all over the
> place,particularly for dates.
Tell me about it... Only one example, I helped a customer with a query a few
w

s ago that had a
date range condition and changing it to a SARG cut down I/O from 777,000 to
2,500.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"OrchidPop" <cristoff@.charter.net> wrote in message
news:1124286059.249743.275670@.g47g2000cwa.googlegroups.com...
> Thanks for the reply. I had hoped cast/convert were not considered true
> functions to block SARGs.
> Actually, I need to check with the original developer of our
> application to find out why convert is used all over the
> place,particularly for dates.
> Thanks!
>