Is there any way to display the string size in a MYSQL result?

6

Is there any way to display the string size in a MYSQL result?

I need to, for example, know the size of a given string returned in a SELECT with group_concat .

What I want would be something like this example:

SELECT "nome", FUNCAO_PARA_OBTER_TAMANHO("nome")

I would like to return something like:

 nome | 4
    
asked by anonymous 28.08.2015 / 18:53

3 answers

8

To know the number of characters can use char_lenght() in the example returns 4 because the return is based on the characters and not on the bytes as it does lenght() which returns 6 (number of bytes);

select CHAR_LENGTH("ação")

select LENGTH("ação")

Example - sqlfiddle

    
28.08.2015 / 19:00
5

I do not know if this is what you are looking for

SELECT nome, LENGTH(nome) as tamanho FROM ....
    
28.08.2015 / 18:59
5

It depends on what measure you want to get.

There are functions LENGTH() and CHAR_LENGTH() .

The LENGTH() function calculates the measure in bytes.

The CHAR_LENGTH() function calculates the measure in characters.

In a somewhat rough comparison with PHP, LENGTH() is equal to strlen() and CHAR_LENGTH() is equal to mb_strlen() .

An interesting curiosity, both functions allow to make a cast of the parameter, defining the type of charset.

select length(_utf8 '言葉'), char_length(_utf8 '言葉')

select length(_utf8 'ação'), char_length(_utf8 'ação')
    
28.08.2015 / 19:00