Home > database >  Char type is a fixed length, why length (name) is not the same as the length?
Char type is a fixed length, why length (name) is not the same as the length?

Time:10-04

I list a column name is char (8), I insert a record name is "Tom," I feel like I check length (name) the result should be 8, because of less than 8, the back fill by five Spaces, this is called a fixed length,
But the fact that I'll find length (name) the result is 3, should not?

CodePudding user response:

Really didn't pay attention to this problem, the understanding of the original and the original poster is the same, but the website on the document is written such
https://dev.mysql.com/doc/refman/5.7/en/char.html
If a given value is stored into the CHAR (4) and a VARCHAR columns (4), the values retrieved from the columns are not always the same because The trailing Spaces are removed from CHAR columns upon retrieval. The following example illustrates this difference:

CodePudding user response:

The length of a CHAR column is fixed to The length that declare The when you create The table. The length can be any value from 0 to 255. The when CHAR values are stored, they are right - padded with Spaces to The specified length. The when CHAR values are retrieved, trailing Spaces are removed unless The PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

Can try to perform set first session sql_mode PAD_CHAR_TO_FULL_LENGTH '='

Try perform length function results
  • Related