I need to query a table for values given a string. The table is case sensitive but I want to do a ToLower() in the comparison.
Suppose I have a classes table with the following data.
class teacher ----------------- Mat101 Smith MAT101 Jones mat101 Abram ENG102 Smith
My query should be something like
Select teacher From classes where lower(class) = 'math101'
Is this the best way to do the comparison?
Update
I have no control over the database or the data. I am a read only consumer.
No; it would be better to improve the data: create a numeric ID that represents these seemingly meaningless variations of class (and probably an associated lookup table to get the ID). Use the ID column in the where clause and you should be hitting an indexed numeric column.
If that’s no an option, consider a function-based index on lower(class).
If that’s not an option, and the question of ‘best’ is strictly relative to performance, consider denormalizing and adding a column that contains lower(class), probably populated with a trigger.
If that’s not an option, update the data so that it’s all lowercase (and take measures to insert/update only lowercase class data).
If you can’t update the data like that, then the answer is ‘maybe’.
In any case, you can’t call it best if you haven’t tested indexing of the column.