

* I take the given keywords and return a value that can be used in a LIKE operation. ' Admin' LIKE '#makeKeywordsLikable( "Jimmy Jane Admin" )#', 'Rebok - Web Properties' LIKE '#makeKeywordsLikable( "Rebok Web" )#', ' Redesign' LIKE '#makeKeywordsLikable( "56 - Design" )#', 'Testing Comments' LIKE '#makeKeywordsLikable( "Test Comment" )#', parameter within a CFQuery tag however, for the sake of the demo and of brevity, CAUTION: Normally, I would ALWAYS USE the CFQueryParam tag when binding any SQL A RegEx can be a combination of different data types such as integer, special characters, Strings, images, etc. target patterns into something a little more "likable" by replacing certain A Regular Expression is popularly known as RegEx, is a generalized expression that is used to match patterns with various sequences of characters. In order to make the SEARCH a little more flexible, we're going to transform the The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. Then, I try to run this UDF against some static strings: To see this in action, I've put together a small ColdFusion demo in which I have a user-defined function (UDF), makeKeywordsLikable() that takes a String an returns a "pattern" that can be used in a LIKE operation.
Mysql like query regex code#
Literally not a day goes by without using RegEx to solve some sort of problem whether in the code itself or in a codebase search in SublimeText. I urge you to watch my video presentation: Regular Expressions, Extraordinary Power. As such, I need to replace _ with a % as well.ĪSIDE: If you're not using RegEx pattern matching on-the-daily in your programming life, you are missing out on a tremendous amount of power. However, the "Word" character class contains the underscore, which is a special character in LIKE operations. Basically, I take any non-Word character and replace it with a %. This gives the user just a little more wiggle-room when trying to match against values in the MySQL database.Īnd, of course, I am using Regular Expressions (RegEx) to massage the inputs. To make a LIKE operation more fuzzy / flexible, all I do is replace some of the input characters with the % wildcard character. And, the percent ( %) matches any arbitrary number of characters, including zero characters. With the SQL LIKE operator, the underscore ( _) matches any single character.


And, as of late, I've been massaging the inputs in order to make the matches even more flexible, allowing for some slightly fuzzy matching in Lucee CFML 5.3.7.47. At InVision, I often use the LIKE operator to allow for light-weight text-based searches. This is especially true if the SQL query in question is already being limited based on an indexed value. While you might stand-up something like Elasticsearch, Lucene, or Solr in order to provide robust and flexible text-based searches in your ColdFusion application, your relational database is more than capable of performing (surprisingly fast) pattern matching on TEXT and VARCHAR fields using the LIKE operator.
