Jamie Kitson
Jamie Kitson

Reputation: 4140

Stop SQL Server Evaluating Useless UPPER/LOWER In WHERE Clause?

it seems that despite the fact that SQL Server does not match on case in a WHERE clause it still honours UPPER/LOWER in a WHERE clause which seems to be quite expensive. Is it possible to instruct SQL Server to disregard UPPER/LOWER in a WHERE clause?

This might seem like a pointless question but it's very nice to be able to write a single query for both Oracle and SQL Server.

Thanks, Jamie

Upvotes: 3

Views: 2842

Answers (2)

Ed Harper
Ed Harper

Reputation: 21505

The short answer to your question is no - you can't have SQL server magically ignore function calls in the WHERE clause.

As others have said, the performance issue is caused because, on SQL Server, using a function in the WHERE clause prevents the use of an index and forces a table scan.

To get best performance, you need to maintain two queries, one for each RDBMS platform (either in your application or in database objects like stored procedures or views). Given that so many other areas of functionality differ between Oracle and SQL Server, you're likely to end up doing it anyway, for something else if not for this.

Upvotes: 4

Rob Farley
Rob Farley

Reputation: 15849

So you mean something like:

WHERE YourColumn = @YourValue collate Latin1_General_BIN

But if you want it to work without the collate keyword, you could just set the collation of the column to something which is case insensitive.

Bear in mind that an index on YourColumn will be using a particular collation, so if you specify the collation in the WHERE clause (rather than on the column itself), an index will be less useful. I liken this to the fact that when I flew in Sweden a few years ago, I couldn't find Vasteras on the map, because the letters I thought were a actually had accents on them and were located at the end of the alphabet. The index in the back of the map wasn't so good when I was trying to use the wrong collation.

Upvotes: 0

Related Questions