Backwards LIKE Statements
Sometimes you need to think backwards.
Here was the problem. I needed to match up some IP address ranges to the company that owns them. Looking for a simple solution to the problem I came up with storing the IP address block patterns in the database as follows:
ip_pattern ---------------- 127.%.%.% 192.168.%.% 10.%.%.%
Any idea why I choose
% as the wildcard?
That's right - it's the wildcard operator in SQL for the
So now when I have have an IP address
192.168.1.1, I can do what I like to call a backwards LIKE query:
SELECT company, ip_pattern FROM company_blocks WHERE '192.168.1.1' LIKE ip_pattern
This works on SQL Server and MySQL, and I would think it should work fine on any database server.
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL - February 18, 2008
- SQL to Select a random row from a database table - September 14, 2005
- SQL Reserved Key Words Checker Tool - March 28, 2005
- Top 10 Reserved SQL Keywords - October 28, 2008
- SQL Case Statement - October 15, 2008
- Java 9 Security Enhancements
- Upcoming CFML Conferences in April 2017
- CFSummit 2016 Slides
- Securing Legacy CFML - dev.Objective() 2016 Slides
- My CFSummit 2015 Slide Decks
- Adding Chrome Custom Search for CFDocs
- Disable Flash Remoting on ColdFusion Servers
- HackMyCF Adds SSL/TLS Scanner