Database Articles

  Home arrow Database Articles arrow Page 4 - Pattern Matching with Strings
DATABASE ARTICLES

Pattern Matching with Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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

    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 10 - Follow our Sitemap