How to perform a case-insensitive database query

By Alvin J. Alexander, devdaily.com

Database FAQ: How do I do a case-insensitive database search?

Answer: When performing a database search using MySQL, PostgreSQl, SQL Server, Oracle, etc., I convert the results of the query to uppercase using the UPPER function, and make sure my search parameters are also in uppercase. Here's an example of how I do this when searching on the first_name field of an Employees database table:

SELECT * 
FROM Employees 
WHERE UPPER(first_name) LIKE '%BILLY%';

Note that I also added in the use of the LIKE clause and wildcard characters around the upercase text "BILLY". Again, this is all standard SQL that should work with any database.

If you're using Java and you need to make sure your text is in uppercase all you have to do is call the toUpperCase() method on your String, like this:

firstName.toUpperCase();

With the query results converted to uppercase and your text in uppercase this problem is solved.


devdaily logo