Database Articles

  Home arrow Database Articles arrow Searching with Strings
DATABASE ARTICLES

Searching with Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-20

    Table of Contents:
  • Searching with Strings
  • 5.15 Using FULLTEXT Searches
  • Using FULLTEXT Searches continued
  • Narrowing the Search

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Searching with Strings


    (Page 1 of 4 )

    In this fifth part of a series of articles that explain how to work with strings and string data, you'll learn how to search for substrings and more. It is excerpted from chapter five of the MySQL Cookbook, Second Edition, written by Paul DuBois (O'Reilly; ISBN: 059652708X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    5.14  Searching for Substrings

    Problem

    You want to know whether a given string occurs within another string.

    Solution

    Use LOCATE().

    Discussion

    The LOCATE() function takes two arguments representing the substring that you’re looking for and the string in which to look for it. The return value is the position at which the substring occurs, or 0 if it’s not present. An optional third argument may be given to indicate the position within the string at which to start looking.

      mysql> SELECT name, LOCATE('in',name), LOCATE('in',name,3) FROM metal;

     

    name

    LOCATE('in',name)  LOCATE('in',name,3) 

     

     copper

     

     gold

     

     iron

     

     lead

     

     mercury

     

     platinum 

     silver

     

     tin

     

     

    LOCATE() uses the collation of its arguments to determine whether the search is case-sensitive. See Recipes 5.6 and 5.9 for information about changing the comparison properties of the arguments if you want to change the search behavior.

    More Database Articles Articles
    More By O'Reilly Media

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap