![]() Second, we insert some blog posts into the posts table by using the INSERT statement: INSERT INTO posts(title,excerpt, content) ) Engine= InnoDB Code language: SQL (Structured Query Language) ( sql ) (We make the posts table as simple as possible for the demonstration purpose).įirst, we create the posts table by using the CREATE TABLE statement: CREATE TABLE posts( Suppose we have a posts table that stores blog posts with four columns postid, title, excerpt and content. SELECT Code language: SQL (Structured Query Language) ( sql ) An application of MySQL string length functions See the following example: SET = CONVERT( 'á' USING utf8) However, if a string has special characters, the result is different. The CHAR_LENGTH and LENGTH returns the same result. Notice that some character sets hold characters whose number of bytes can be varied e.g., for the utf8 character set: SET = CONVERT( 'MySQL String Length' USING utf8) The latin1 character set contains 1-byte characters therefore, its length in bytes and its length in character are equal. ![]() We use the latin1 character set for the string. The following statements demonstrate how the LENGTH and CHAR_LENGTH functions that work with 1-byte characters: SET = CONVERT( 'MySQL string length' USING latin1) Because the string contains 2-byte characters, its length in character is 19, while its length in bytes is 38. Second, we use the CHAR_LENGTH and LENGTH functions to get the length of the string in bytes and in characters.First, we convert the MySQL String Length string into ucs2 character set, which is UCS-2 Unicode that holds 2-byte characters.SELECT Code language: SQL (Structured Query Language) ( sql ) Let’s take a look at the following statements: SET = CONVERT( 'MySQL String Length' USING ucs2) You use the CHAR_LENGTH function to get the length of a string measured in characters as follows: CHAR_LENGTH(str) Code language: SQL (Structured Query Language) ( sql ) Examples of LENGTH and CHAR_LENGTH functions To get the length of a string measured in bytes, you use the LENGTH function as follows: LENGTH(str) Code language: SQL (Structured Query Language) ( sql ) However, if a string contains multi-byte characters, its length in bytes is typically greater than its length in characters. If a string contains 1-byte characters, its length measured in characters and its length measured in bytes are equal. The Maxlen column stores the number of bytes for character sets. In MySQL, a string can be in any character set. SHOW CHARACTER SET Code language: SQL (Structured Query Language) ( sql ) ![]() You use the SHOW CHARACTER SET statement to get all character sets supported by MySQL database server. MySQL supports various character sets such as latin1, utf8, etc. Summary: in this tutorial, you will learn about MySQL string length functions that allow you to get the length of strings measured in bytes and in characters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |