Database Articles
  Home arrow Database Articles arrow Page 3 - Working with 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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

Working with Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2007-08-24

    Table of Contents:
  • Working with Strings
  • 5.1 String Properties
  • String Properties continued
  • 5.2 Choosing a String Data Type

  • 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


    Working with Strings - String Properties continued


    (Page 3 of 4 )

    The following example illustrates how collation affects sort order. Suppose that a table contains a latin1 string column and has the following rows:

      mysql> CREATE TABLE t (c CHAR(3) CHARACTER SET latin1);
      mysql> INSERT INTO t (c) VALUES('AAA'),('bbb'),('aaa'),('BBB');
      mysql> SELECT c FROM t;

      +------+
      | c     |
      +-------+
      | AAA   |
      | bbb   |
      | aaa   |
      | BBB   |
      +-------+

    By applying the COLLATE operator to the column, you can choose which collation to use for sorting and thus affect the order of the result:

    • A case-insensitive collation sorts a and A together, placing them before b and B. However, for a given letter, it does not necessarily order one lettercase before another, as shown by the following result:

        mysql> SELECT c FROM t ORDER BY c COLLATE latin1_swedish_ci;

       
      +------
      +
        | c     |
        +-------+
        | AAA   |
        | aaa   |
        | bbb   |
        | BBB   |
        +-------+
       
    • A case-sensitive collation puts A and a before B and b, and sorts uppercase before lowercase:

        mysql> SELECT c FROM t ORDER BY c COLLATE latin1_general_cs;

       +------
      +
       | c     |
       +-------+
       | AAA   |
       | aaa   |
       | BBB   |
       | bbb   |
       +-------+
       
    • A binary collation sorts characters using their numeric values. Assuming that uppercase letters have numeric values less than those of lowercase letters, a binary collation results in the following ordering:

        mysql> SELECT c FROM t ORDER BY c COLLATE latin1_bin;

       
      +------
      +
        | c     |
        +-------+
        | AAA   |
        | BBB   |
        | aaa   |
       
      | bbb   |
        +-------+


      Note that, because characters in different lettercases have different numeric values, a binary collation produces a case-sensitive ordering. However, the order is different than that for the case-sensitive collation.

    You can choose a language-specific collation if you require that comparison and sorting operations use the sorting rules of a particular language. For example, if you store strings using the utf8 character set, the default collation (utf8_general_ci) treats ch and ll as two-character strings. If you need the traditional Spanish ordering that treats ch and ll as single characters that follow c and l, respectively, use the utf8_spanish2_ci collation. The two collations produce different results, as shown here:

      mysql> CREATE TABLE t (c CHAR(2) CHARACTER SET utf8);
      mysql> INSERT INTO t (c) VALUES('cg'),('ch'),('ci'),('lk'),('ll'),('lm');
      mysql> SELECT c FROM t ORDER BY c COLLATE utf8_general_ci;

      +------+
      | c     |
      +-------+
      | cg    |
      | ch    |
      | ci    |
      | lk    |
      | ll    |
      | lm    |
      +-------+
      mysql> SELECT c FROM t ORDER BY c COLLATE utf8_spanish2_ci;

      +------+
      | c     |
      +-------+
      | cg    |
      | ci    |
      | ch    |
      | lk    |
      | lm    |
      | ll    |
      +-------+

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek