Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server ignore case in a where expression

How do I construct a SQL query (MS SQL Server) where the "where" clause is case-insensitive?

SELECT * FROM myTable WHERE myField = 'sOmeVal' 

I want the results to come back ignoring the case

like image 290
Raul Agrait Avatar asked Aug 03 '09 20:08

Raul Agrait


People also ask

How do you disregard a case in SQL?

Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

Does SQL Server ignore case?

SQL Server is, by default case insensitive; however, it is possible to create a case sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine a database or database object is by checking its “COLLATION” property and look for “CI” or “CS” in the result.

How do you perform a case insensitive comparison in SQL?

To do a case-insensitive comparison, use the ILIKE keyword; e.g., column ILIKE 'aBc' and column ILIKE 'ABC' both return TRUE for 'abc' . In contrast, MySQL and MS SQL Server have case-insensitive behaviors by default. This means WHERE column = 'abc' returns TRUE for e.g., 'abc' , 'ABC' , or 'aBc' .

Does like in SQL ignore case?

SQL syntax is generally case insensitive. If LIKE and like lead to different results that means that your SQL syntax is case sensitive, which I've never seen before. The documentation for Hibernate says " sELEct is the same as SELECT ".


2 Answers

In the default configuration of a SQL Server database, string comparisons are case-insensitive. If your database overrides this setting (through the use of an alternate collation), then you'll need to specify what sort of collation to use in your query.

SELECT * FROM myTable WHERE myField = 'sOmeVal' COLLATE SQL_Latin1_General_CP1_CI_AS 

Note that the collation I provided is just an example (though it will more than likely function just fine for you). A more thorough outline of SQL Server collations can be found here.

like image 173
Adam Robinson Avatar answered Sep 18 '22 11:09

Adam Robinson


Usually, string comparisons are case-insensitive. If your database is configured to case sensitive collation, you need to force to use a case insensitive one:

SELECT balance FROM people WHERE email = '[email protected]'   COLLATE SQL_Latin1_General_CP1_CI_AS  
like image 26
Andrejs Cainikovs Avatar answered Sep 19 '22 11:09

Andrejs Cainikovs