Database Articles
  Home arrow Database Articles arrow Page 4 - Pattern Matching with Strings
Try It Free
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

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

    Table of Contents:
  • Pattern Matching with Strings
  • Pattern Matching with Regular Expressions continued
  • 5.12 Controlling Case Sensitivity in Pattern Matching
  • 5.13 Breaking Apart or Combining Strings
  • Breaking Apart or Combining Strings

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Try It Free
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Pattern Matching with Strings - 5.13 Breaking Apart or Combining Strings


    (Page 4 of 5 )

    Problem

    You want to extract a piece of a string or combine strings to form a larger string.

    Solution

    To obtain a piece of a string, use a substring-extraction function. To combine strings, use CONCAT().

    Discussion

    Strings can be broken apart by using appropriate substring-extraction functions. For example, LEFT(), MID(), and RIGHT() extract substrings from the left, middle, or right part of a string:

      mysql> SELECT name, LEFT(name,2), MID(name,3,1), RIGHT(name,3) FROM metal;

     

    name

     LEFT(name,2) | MID(name,3,1)  RIGHT(name,3)

     

     copper

    co

     p

    per

     

     gold

     go

     l

     old

     

     iron

     ir

     o

     ron

     

    lead

     le

     a

    ead

     

     mercury

     me

     r

     ury

     

    platinum | pl

     a

     num

     

     silver

     si

     l

    ver

     

     tin

     ti

     n

    tin

     

     

    For LEFT() and RIGHT(), the second argument indicates how many characters to return from the left or right end of the string. For MID(), the second argument is the starting position of the substring you want (beginning from 1), and the third argument indicates how many characters to return.

    The SUBSTRING() function takes a string and a starting position, returning everything to the right of the position. MID() acts the same way if you omit its third argument because MID() is actually a synonym for SUBSTRING():

      mysql> SELECT name, SUBSTRING(name,4), MID(name,4) FROM metal;

     

     

     name

    SUBSTRING(name,4) MID(name,4)

     

    copper

    | per

    | per

     

    gold

     d

     d

     

    iron

     n

     n

     

    lead

     d

     d

     

    mercury

    cury

    cury

     

    platinum tinum

     tinum

     

     silver

     ver

    ver

     

     

    tin

     

    Use SUBSTRING_INDEX(str,c,n) to return everything to the right or left of a given character. It searches into a string str  for the n-th occurrence of the character c and returns everything to its left. If n  is negative, the search for c  starts from the right and returns everything to the right of the character:

      mysql> SELECT name,
         -> SUBSTRING_INDEX(name,'r',1),
         -> SUBSTRING_INDEX(name,'i',-1)
         -> FROM metal;

     

     

     name

     SUBSTRING_INDEX(name,'r',1) SUBSTRING_INDEX(name,'i',-1)

     

    copper

    coppe

    copper

     

    gold

    gold

    gold

     

    iron

    i

    ron

     

     lead

    lead

     lead

     

     mercury

     me

    mercury

     

     platinum  platinum

     num

     

     silver

     silve

     lver

     

     tin

     tin

    n

     

     

    Note that if there is no n-th occurrence of the character, SUBSTRING_INDEX() returns the entire string. SUBSTRING_INDEX() is case-sensitive.

    Substrings can be used for purposes other than display, such as to perform comparisons. The following statement finds metal names having a first letter that lies in the last half of the alphabet:

      mysql> SELECT name from metal WHERE LEFT(name,1) >= 'n';
      +----------+
      | name     |
      +----------+
      | platinum |
      | silver   |
      | tin      |
      +----------+

    More Database Articles Articles
    More By O'Reilly Media


     

    Buy this book now. This article is excerpted from chapter five of the MySQL Cookbook, Second Edition, written by Paul DuBois (O'Reilly; ISBN: 059652708X). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway