SQL Server Text Matching Is Case INSENSITIVE

Posted May 23, 2007 at 12:33 PM

Tags: SQL

I am officially retarded. I can't believe that I didn't know that the text equals operator "=" in SQL Server queries was case insensitive. I am not sure where I got this in my head, but I always assumed that "=" was case sensitive for text comparison and LIKE was case INsensitive. But in reality, these three SQL statements all return the same result:

 Launch code in new window » Download code as text file »

  • SELECT
  • id
  • FROM
  • address
  • WHERE
  • street1 = '123 Street St'
  •  
  •  
  • SELECT
  • id
  • FROM
  • address
  • WHERE
  • street1 = '123 STREET ST'
  •  
  •  
  • SELECT
  • id
  • FROM
  • address
  • WHERE
  • street1 = '123 StReEt sT'

Ugggg! To think of the times I have used LOWER() on a field to search for case insensitive text matches. Thank God I don't deal with too many queries that needed to do case insensitive searches (at least the fallout of my ignorance will not wreak too much havoc).

I just hate making mistakes like that or finding such glaring knowledge gaps :(

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page





Reader Comments

May 23, 2007 at 12:47 PM // reply »
6 Comments

If you are using SQL Server, check out the COLLATE clause to make searches case sensitive....


May 23, 2007 at 1:12 PM // reply »
56 Comments

welcome Ben, we've been waiting for you :P


May 23, 2007 at 2:01 PM // reply »
3 Comments

If I recall correctly on SQL server 2000, when installing you can decide if your install to be case sensitive can't remember if this is per server or database though.


May 23, 2007 at 2:25 PM // reply »
7,207 Comments

@Bob,

I will look into that. I have heard of using COLLATE in order to search with extended ASCII characters (I think) but I have never actually used it.

@Tony,

The stars were finally in the right alignment... I have arrived.

@Kola,

Thanks for the heads up. I will talk to my boss about it as he administers the database server installations. And, when I create a new database, I just use the default settings - I have never seen what kinds of things can be set.


May 23, 2007 at 2:27 PM // reply »
2 Comments

You might need to change the collation of the database (not reliable in SQL Server 2000) or reinstall SQL Server 2000 with a case sensitive collation.

Take a look at this link:

http://expertanswercenter.techtarget.com/eac/knowledgebaseAnswer/0,295199,sid63_gci1054949,00.html


May 23, 2007 at 2:31 PM // reply »
7,207 Comments

@Dimitar,

You are just saying to do that IF I want to change from case insensitive to case sensitive, right?


May 23, 2007 at 2:50 PM // reply »
38 Comments

I thought all databases were this way, except for PostGreSQL. I've read that this is one of the big things people /don't'/ like about PGS is that SLQ queries are case sensitive by default. Someone correct me if I'm wrong.


May 23, 2007 at 3:55 PM // reply »
2 Comments

By default SQL Server 2000 (and 2005) installs with a case insensitive collation. It's the least problematic anyway but the most restricted in order of dev options like case sensitiveness, alphabetic order, etc.

So I guess when installed your RDBS you left it with its default options. Chances to convert ONLY your db into a case-sensitive collation are high, the opposite is alomost impossible though (from a case-seinsitve db collation to case insensitive db collation).

A good solution (my opinion) is just download SQL Server 2000 Books Online

http://www.microsoft.com/downloads/details.aspx?FamilyID=A6F79CB1-A420-445F-8A4B-BD77A7DA194B&displaylang=en

and read about the installation options. Or goolge, there are plenty of ideas.

The best advice though is simply switch to SQL Server 2005 (if you have an option for this), it's much less problematic, have tons of options to fix such issues without the need to reinstall the server or play around with the system tables, etc. Before that though don;t forget to back up your databases, right :-)


May 23, 2007 at 9:22 PM // reply »
1 Comments

Oracle is case sensitive too. Remember, the LIKE clause is for string comparison. It does not deal with case directly, as it is designed to find a string pattern. In fact, LIKE should be case sensitive. Honestly, I feel that all RDBMS should process everything with case sensitivity. This can help you find poorly formatted or erroneous data. jmtc.


May 24, 2007 at 8:08 AM // reply »
1 Comments

You don't need to reininstall to do a case sensitve search. Here is an example:

SELECT Col1, Col2, Col3
FROM MyTable
WHERE Col3 COLLATE SQL_Latin1_General_CP1_CS_AS LIKE '%foo%'

Here you set Col3 case sensitive. You can also use the "=" statement in stead of LIKE.


May 24, 2007 at 8:12 AM // reply »
7,207 Comments

I see that this COLLATE stuff has a lot of power. I need to look into that more.


Aug 1, 2007 at 9:50 AM // reply »
1 Comments

You can also make the comparison on a varbinary conversion of the strings

WHERE convert(varbinary, column1) = convert(varbinary, @param)

Seen here
http://www.devx.com/tips/Tip/13043


Sep 13, 2007 at 1:52 PM // reply »
3 Comments

Thanks Jorrit very interesting and straight forward answer worked for me like a charm


Sep 9, 2009 at 11:50 AM // reply »
1 Comments

Equally retarded here. Oh well, it's nice to know we still have something to learn.


Dec 3, 2009 at 7:18 AM // reply »
2 Comments

CAST(mycolumn AS VARBINARY(10)) = CAST('MyValue' AS VARBINARY(10))


Dec 3, 2009 at 7:20 AM // reply »
2 Comments

SELECT * FROM MyTable
WHERE (CAST(ColuumnName AS VARBINARY(10)) = CAST('MySearchValue' AS VARBINARY(10)))


Dec 18, 2009 at 1:34 AM // reply »
2 Comments

Hi,
COLLATE is the Keyword which help you to compare case sensitivity.
Syntax: COLLATE (Collation Value)
EX: COLLATE SQL_Latin1_General_CP1_CS_AS
you should keep this part between the comparision statement
EX: SELECT * FROM USERS
WHERE USER_NAME COLLATE SQL_Latin1_General_CP1_CS_A = 'Your String'....

Cheers,
Rajendra Prasad panchati.


Dec 18, 2009 at 1:40 AM // reply »
2 Comments

Regarding above post,
You can get the Collation Value from the properties of your database on which you are currently working..
Your Database-->Properties-->General(Under Maintenance).


Jan 5, 2010 at 9:34 AM // reply »
7,207 Comments

@Rajendra,

Collation is something that I've seen before, but never really understood. I should take some time to learn more about it.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 4:14 AM
Ask Ben: Converting a Query to an Array
Exellent script! Tried to make it shorter, gained approx 10% performance improvement :) [CODE] <cffunction name="queryToArray" access="public" returntype="array" output="false"hint="This turns a q ... read »
Feb 9, 2010 at 3:16 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Just a quick fix for the code example above: $('.panel').height() should instead be... $('.panel').outerHeight() ... read »
Feb 9, 2010 at 3:09 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a p ... read »
Feb 9, 2010 at 12:57 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
Have you got any reference to code explaining the procedure to use cfdocument, file to disk and attach to email? ... read »
Feb 8, 2010 at 11:27 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Randi, While I can appreciate your specific situation, I personally am not a fan of a situation where people have the option to run their own ad-hoc reports on the database. This is, honestly, a r ... read »
Feb 8, 2010 at 11:15 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Candice, No problem - glad you got it figured out. @David, To view the XLS document as an XML file, if I remember correctly, I actually opened up the document in Excel and then went "File > ... read »
Feb 8, 2010 at 11:11 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob, As @Julian pointed out, you need to enable multiple queries in order to run queries containing semi-colons. For more info on that, take a look at this post: http://www.bennadel.com/blog/120 ... read »
Feb 8, 2010 at 11:08 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
@Robert, Awesome. When you start reading it, I'd love for you to drop by and share your comments. I happen to love the book and am always happy to have a good conversation about it. ... read »