Database Articles
  Home arrow Database Articles arrow Working with Cases of Strings
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 
IBM Developerworks
 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

Working with Cases of Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-09-07

    Table of Contents:
  • Working with Cases of Strings
  • 5.8 Converting the Lettercase of a Stubborn String
  • 5.9 Controlling Case Sensitivity in String Comparisons
  • 5.10 Pattern Matching with SQL Patterns

  • 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
     
     
     
    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

    Working with Cases of Strings
    (Page 1 of 4 )

    In this third part of a series of articles that explain how to work with strings and string data, you'll learn about cases and case sensitivity. 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.7  Converting the Lettercase of a String

    Problem

    You want to convert a string to uppercase or lowercase.

    Solution

    Use the UPPER() or LOWER() function. If they don’t work, see Recipe 5.8.

    Discussion

    The UPPER() and LOWER() functions convert the lettercase of a string:

      mysql> SELECT thing, UPPER(thing), LOWER(thing) FROM limbs;

     

     thing UPPER(thing)  LOWER(thing)

     

    human  HUMAN  human

     insect INSECT insect

     squid SQUID  squid 

    octopus  OCTOPUS  octopus

     fish FISH fish 

    centipede CENTIPEDE centipede

    table TABLE  table

     armchair  ARMCHAIRarmchair

     phonograph PHONOGRAPH phonograph

     tripod TRIPOD  tripod 

     Peg Leg Pete  PEG LEG PETE peg leg pete

     space alien  SPACE ALIEN  space alien 

     

    To convert the lettercase of only part of a string, break it into pieces, convert the relevant piece, and put the pieces back together. Suppose that you want to convert only the initial character of a string to uppercase. The following expression accomplishes that:

      CONCAT(UPPER(LEFT(str,1)),MID(str,2))

    But it’s ugly to write an expression like that each time you need it. For convenience, define a stored function:

      mysql> CREATE FUNCTION initial_cap (s VARCHAR(255))
         -> RETURNS VARCHAR(255) DETERMINISTIC
         -> RETURN CONCAT(UPPER(LEFT(s,1)),MID(s,2));

    Converting the Lettercase of a String

    You can then capitalize initial characters more easily like this:

      mysql> SELECT thing, initial_cap(thing) FROM limbs;

      +--------------+--------------------+
      | thing        | initial_cap(thing) |
      +--------------+--------------------+
      | human        | Human              |
      | insect       | Insect             |
      | squid        | Squid              |
     
    | octopus      | Octopus            |
      | fish         | Fish               |
      | centipede    | Centipede          |
      | table        | Table              |
      | armchair     | Armchair           |
      | phonograph   | Phonograph         |
      | tripod       | Tripod             |
      | Peg Leg Pete | Peg Leg Pete       |
      | space alien  | Space alien        |
      +--------------+--------------------+

    For more information about writing stored functions, see Chapter 16.

    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 3 hosted by Hostway