CHARACTER_LENGTH : Number of characters present in a string

We can find out the length of the data or characters actually present in a field of a MySQL table by using character_length function.
SELECT CHARACTER_LENGTH('Welcome to plus2net')
Output is 19

Difference between LENGTH & CHARACTER_LENGTH

CHARACTER_LENGTH returns the number of characters used and it counts double-byte characters as a single character.

LENGTH returns number of bytes stored. Double byte characters are counted as 2.

Example: Euro sign occupies 3 bytes
SELECT LENGTH(_utf8 '€'), CHARACTER_LENGTH(_utf8 '€')
Output is
3,  1

Length of data stored in column

For example, we are storing titles for an image in a MySQL table. We have assigned 100 as the length of this varchar field. Here 100 is the maximum length of title allowed but it is not always that we will be using a total of 100 characters. So how to know how many characters we have used? Now let us find out the length of the title used for the image with id 47. Here is the query

SELECT character_length(title) as no, title  FROM `photo_img`  where img_id=47
Without giving any where clause we can display the length of all the titles along with the title for the total table. Let us try to find out what are the titles using maximum space or the titles of maximum length. This will display records with maximum character in the title field. This we will manage by using sql order by clause. Check this query.
SELECT character_length(title) as no, title  FROM `photo_img`  order by no desc
SQL String References SQL to get length of string substring_index to get part of string using delimiter

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com

    Post your comments , suggestion , error , requirements etc here





    SQL Video Tutorials










    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer