Database Articles

  Home arrow Database Articles arrow Page 3 - Connections, Character Sets and String...
DATABASE ARTICLES

Connections, Character Sets and Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-08-31

    Table of Contents:
  • Connections, Character Sets and Strings
  • 5.4 Writing String Literals
  • 5.5 Checking a String’s Character Set or Collation
  • 5.6 Changing a String’s Character Set or Collation

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Connections, Character Sets and Strings - 5.5 Checking a String’s Character Set or Collation


    (Page 3 of 4 )

    Problem

    You want to know the character set or collation of a string.

    Solution

    Use the CHARSET() or COLLATION() function.

    Discussion

    If you create a table using the following definition, you know that values stored in the column will have a character set of utf8 and a collation of utf8_danish_ci:

      CREATE TABLE t (c CHAR(10) CHARACTER SET utf8 COLLATE utf8_danish_ci);

    But sometimes it’s not so clear what character set or collation applies to a string. Server configuration affects literal strings and some string functions, and other string functions return values in a specific character set. Symptoms that you have the wrong character set or collation are that a collation-mismatch error occurs for a comparison operation, or a lettercase conversion doesn’t work properly. This section shows how to check what character set or collation a string has. Recipe 5.6 shows how to convert strings to a different character set or collation.

    To find out what character set or collation a string has, use the CHARSET() or COLLATION() function. For example, did you know that the USER() function returns a Unicode string?

      mysql> SELECT USER(), CHARSET(USER()), COLLATION(USER());

     

     USER() CHARSET(USER())  COLLATION(USER())

     

     cbuser@localhost utf8 utf8_general_ci

     


    String values that take their character set and collation from the current configuration may change properties if the configuration changes. This is true for literal strings:

      mysql> SET NAMES 'latin1';
      mysql>
    SELECT CHARSET('abc'), COLLATION('abc');
      +----------------+-----------------

      | CHARSET('abc')   | COLLATION('abc')   |
      +------------------+--------------------+
      | latin1           | latin1_swedish_ci  |
      +------------------+--------------------+ 

      mysql>
    SET NAMES latin7 COLLATE 'latin7_bin';
      mysql>
    SELECT CHARSET('abc'), COLLATION('abc');
      +----------------+------------------
    +
      | CHARSET('abc')   | COLLATION('abc')    |
      +------------------+---------------------+
      | latin7           | latin7_bin          |   +------------------+---------------------+

    For a binary string, the CHARSET() or COLLATION() functions return a value of binary, which means that the string is compared and sorted based on numeric byte values, not character collation values. Several functions return binary strings, such as MD5() and PASSWORD():

      mysql> SELECT CHARSET(MD5('a')), COLLATION(MD5('a'));

     

    CHARSET(MD5('a')) COLLATION(MD5('a')) 

     

     binary  binary

     

      mysql> SELECT CHARSET(PASSWORD('a')), COLLATION(PASSWORD('a'));

     

     

     CHARSET(PASSWORD('a')) COLLATION(PASSWORD('a')) 

     

    binary  binary 

     

    It can be useful to know that a function or string expression produces a binary string if you’re trying to perform lettercase conversion on the result and it’s not working. See Recipe 5.8 for details.

    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