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

Asked

Viewed 2,715 times

6

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

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

What I want would be more or less like this example:

SELECT "nome", FUNCAO_PARA_OBTER_TAMANHO("nome")

I’d like to return something like:

 nome | 4

3 answers

9


To know the number of characters you 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() that returns 6(number of bytes);

select CHAR_LENGTH("ação")

select LENGTH("ação")

Example - sqlfiddle

  • Interesting. It seems that it is more convenient because of the UTF-8 and the like!

5

I don’t know if that’s what you’re looking for

SELECT nome, LENGTH(nome) as tamanho FROM ....

5

It depends on which measure you want to get.

There are the functions LENGTH() and CHAR_LENGTH().

The function LENGTH() calculate the measure in bytes.

The function CHAR_LENGTH() calculates the measure in characters.

Somewhat coarse comparison with PHP, LENGTH() is equivalent tostrlen() and CHAR_LENGTH() is equivalent to mb_strlen().

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

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

select length(_utf8 'ação'), char_length(_utf8 'ação')
  • Thanks for the explanation. Spared me from asking a question. When I did LENGTH('é') returned 2. Should return 1. But with the CHAR_LENGTH was just right.

  • 2

    length() returns the actual size in bytes, which is very different from the number of characters. I think it’s best not to talk too much about bytes or else the kib guy, Mib, Gib.. rsrsrssr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.