Backwards LIKE Statements


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Sometimes you need TO think backwards.
  2.  
  3. 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:
  4.  
  5. ip_pattern
  6. ----------------
  7. 127.%.%.%
  8. 192.168.%.%
  9. 10.%.%.%
  10.  
  11. Any idea why I choose % AS the wildcard?
  12.  
  13. That's right - it's the wildcard operator IN SQL FOR the LIKE statement.
  14.  
  15. 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:
  16.  
  17. SELECT company, ip_pattern
  18. FROM company_blocks
  19. WHERE '192.168.1.1' LIKE ip_pattern
  20.  
  21. This works ON SQL Server AND MySQL, AND I would think it should WORK fine ON any DATABASE server.

URL: http://www.petefreitag.com/item/619.cfm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.