Search code examples
regexcoldfusioncfquery

Search result that has a C and a number after the C in the Part Number Field in ColdFusion


I have a search results page that lists part numbers that engineers have to work on, but I only want part numbers displayed on the results page that have a C and then a number after the C in the part number. This is done in ColdFusion. How do I write this in the Where statement in my CFQuery? If I write Where Part_Number Like 'C%', it lists all the parts that have anything after a C in the part number like CBT-BGA. I want it to find only part numbers like this: C4560. I also tried this: Where Part_Number Like '^C[0-9]', but this doesn't show any results. Thanks for your help.


Solution

  • We need to know which database you're using to give you a proper answer. The fact that you're using ColdFusion has no bearing on the structure of the SQL you need to generate.

    If you're using SQL Server, SQLShack says you should be able to use the like operator with some minimal pattern matching:

    SELECT whatever FROM someTable WHERE someColumn LIKE '[C][0-9]%'
    

    The syntax will likely be different for MySQL or Postgresql, but some quick searches should turn up the proper approach.