SQL Server Text Matching Is Case INSENSITIVE

Posted May 23, 2007 at 12:33 PM by Ben Nadel

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:

  • 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 :(




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 »
10,640 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 »
10,640 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 »
10,640 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 »
10,640 Comments

@Rajendra,

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


Feb 10, 2010 at 6:03 AM // reply »
1 Comments

select * from tablename where Username=@Username and cast(Password as varbinary(15))=cast(@Password as varbinary(15))


Apr 28, 2010 at 6:41 AM // reply »
1 Comments

Ben,This has worked for me:

SELECT * FROM Report_Lender_View_Field
WHERE (CAST(name_of_col AS VARBINARY(10)) = CAST('searchstring' AS VARBINARY(10)))

As developers we might not have access for the changing collate stuff.

Cheers,
Ashok


May 18, 2010 at 9:24 PM // reply »
10,640 Comments

@Jagadish, @Ashok,

I've never understood the varbinary data type. I've used it before when I find it in an example; but, I don't fully get what it is. Based on the name, I understand that it is a variable-length binary data value; but, I am not sure why converting things to varbinary has "unexpected" benefits (as far as I see).


May 19, 2010 at 1:21 PM // reply »
1 Comments

Casting to varbinary will convert the string in binary data. Since 'A' and 'a' are diffents characters, the cast will result in different binary data, so case sensitive comparison.
Not really unexpected behavior.


May 19, 2010 at 1:23 PM // reply »
10,640 Comments

@Pascal,

Ah, I think I had it backwards in my mind. I thought people were casting to varbinary to do case INsensitive comparison. Yeah, sensitive comparison makes more sense. Sorry for the confusion.


Apr 12, 2011 at 3:46 AM // reply »
1 Comments

@Jorrit, Thanks a bunch


Jun 2, 2011 at 5:06 AM // reply »
2 Comments

Hi,

One small question here. You have mentioned usage of:
(a) VARBINARY
(b) COLLATE SQL_Latin1_General_CP1_CS_AS

Could you also please state - Of these two models, which is better and why?

Thanks


Jun 3, 2011 at 10:53 PM // reply »
10,640 Comments

@Manoj,

I'm afraid that question is beyond my understanding. From what I know, a varbinary is basically a string value stored as a byte-array rather than a character array.... or something like that. Those data types are outside my experience. Sorry.


Nov 30, 2011 at 7:43 AM // reply »
1 Comments

Bests are always simple!

WHERE (LOWER(COLUMN_NAME) = LOWER(@VALUE))


Jan 19, 2012 at 3:56 AM // reply »
1 Comments

Thanks for this post...


Jan 19, 2012 at 5:34 AM // reply »
2 Comments

Thanks @Ben for trying. Maybe someone else reading your blog post would help me understand.

Regards.


Jan 19, 2012 at 4:41 PM // reply »
5 Comments

The collation determines the rules used to compare different strings(case-sensitivity/ ordering of accents)

The varbinary treats the data as raw bytes and comparison will be done on value.

If you are using strings, you should go with collation as it offers you control over different aspects of the ordering. By convertng to varbinary you will only order your strings on byte value and might end with 'ç' way after 'z' when it is not your intention.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »