SQL LIKE Clause Case Sensitive in ColdFusion MX Query-of-Query

Posted June 14, 2006 at 8:10 AM

Tags: ColdFusion, SQL

I just ran into an interesting issue when querying the file system. I was trying to get only parts of the file set by using a ColdFusion MX query-of-queries (QoQ) and limiting the results based on file extensions. For example, I had a query like this:

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

  • <cfquery name="qFiles" dbtype="query">
  • SELECT
  • name
  • FROM
  • qAllFiles
  • WHERE
  • name LIKE '%.jpg'
  • </cfquery>

In the above example, I was trying to get a sub-set of files that were JPGs. This worked great until I notices that I wasn't get any of the files that had uppercase extensions like Blam. I had thought that the SQL LIKE clause was NOT case sensitive; however, I usually work directly in SQL so maybe there was a difference in QoQ.

After running these tests:

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

  • <cfquery name="qSQL" datasource="#REQUEST.DSN.Source#">
  • SELECT
  • id,
  • name
  • FROM
  • blog_entry
  • WHERE
  • name LIKE '%T%'
  • </cfquery>
  •  
  • <!--- qSQL : Returns 15 Records. --->
  •  
  • <cfquery name="qQOQ" dbtype="query">
  • SELECT
  • id,
  • name
  • FROM
  • qSQL
  • WHERE
  • name LIKE '%T%'
  • </cfquery>
  •  
  • <!--- qQOQ : Returns 7 Records. --->

I discovered that there was a difference in how LIKE was handled. In theory, the two queries above should have returned the same result set as they were running the same query (the only difference being that the first query would return a sub-set of the universe of records while the second query would return the entire available result set). If you are running a query in the database then yes, the LIKE clause is case insensitive. If you are running a query of queries in ColdFusion MX 7, then yes, the LIKE clause is case sensitive.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Apr 19, 2007 at 4:03 PM // reply »
1 Comments

I just encountered this issue and found a solution that allows for case-insensitive searching with QofQ's. I know this is an old blog entry, but I thought I'd share it anyway.

You can use a combination of LOWER() and LCase() to address this issue.

In your example, your query would change to:

SELECT id, name
FROM qSQL
WHERE LOWER(name) LIKE '%#LCase("T")#%'

Now it doesn't make much sense just using "T", but if you were using a variable, which is more likely, then it will convert your variable to lower case, and also convert the contents of the column (in your example "name") to lower case as well. This version would look like:

SELECT id, name
FROM qSQL
WHERE LOWER(name) LIKE '%#LCase(my_Variable)#%'

Cheers,
Bob


Apr 19, 2007 at 5:58 PM // reply »
7,572 Comments

@Bob,

That is a good suggestion. Thanks for posting it up here.


Aug 15, 2007 at 10:34 AM // reply »
4 Comments

Superb post!

Thanks for the tip. I only just came across this issue as I don't often use QoQ.. I was quite stumped for a few minutes trying to figure out why the search wasn't working as expected.

2 minutes later and solution found via Google.. Thanks for this blog (which has helped me more than once via the essential Google programming problem search) and the commenter for the fix.


Aug 15, 2007 at 10:37 AM // reply »
7,572 Comments

@James,

No problem. Just glad to help. If you ever hit any road blocks, feel free to contact me.


Nov 17, 2008 at 9:27 AM // reply »
1 Comments

I inherited some cold that uses this technique and it is not working.

Any suggestions?


Jan 10, 2009 at 1:52 AM // reply »
1 Comments

Hi,

Thanks for post this excellent tip here.


Feb 12, 2009 at 4:19 PM // reply »
1 Comments

Nice one, Ben. This had me tripped up for a bit today. This blog entry lets me know I'm not alone, even if I am three years behind the curve.

Josh


Jun 3, 2009 at 3:21 AM // reply »
9 Comments

I just finished modifying an object factory to fail over to a WDDX file of cached serialized queries when the database is unavailable (fun stuff, maybe worth a blog post!) and encountered this, since the non-DB mode was doing all the queries in QoQ mode. What a pain. I wouldn't mind the case sensitivity if I could just turn it off perhaps with a cfquery tag attribute.

Thanks for the post man.


Jun 3, 2009 at 11:07 AM // reply »
7,572 Comments

@Josh,

Yeah, serializing data for queries can be a pain! Especially since the exact data types are not brought back through (from what I can remember, although that might not be completely accurate). When I stored to WDDX, I remember having to also store meta data about the query to have it deserialize properly (via manually construction). Total pain!


Sep 8, 2009 at 6:07 AM // reply »
2 Comments

is a like also posible in a case ?


Sep 27, 2009 at 4:56 AM // reply »
1 Comments

Hi Ben,

I found this post very useful thanks. Over the past three or four years since I started learning CFML I've come across solutions to my programming problems on your blog countless times but never seem to get round to saying thanks... You're a real asset to the CFML community. Thanks very very much for all the past posts, this post and all the other posts I'll no doubt encounter in the future!

Ben Roberts
(Probably the only person using CFML in the Czech Republic)


Sep 29, 2009 at 8:20 AM // reply »
7,572 Comments

@Ben,

Ben (awesome name by the way), I'm always glad to help. I just hope people get as much out of it as I do - writing all this stuff really helps me learn.


Feb 26, 2010 at 7:03 AM // reply »
2 Comments

Hi Ben,

I found another similar difference in QoQ.

Like '%a%' is treated differently in a database query and a QoQ. Database query will return anything with an 'a' in it, QoQ will not return results where the only occurrence is that they start with 'a'.

ie. Database query will return 'apple, banana'
QoQ will only return 'banana'.

Great Blog by the way, always look for it first in search results.


Feb 26, 2010 at 7:09 AM // reply »
2 Comments

Ignore that last one, I was getting my queries case mixed up. Sorry.


Feb 27, 2010 at 12:01 PM // reply »
7,572 Comments

@Robin,

No problem - glad you got it sorted.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »