"MySQL" calculates the length of the text type field using the length and cast functions

Source: Internet
Author: User

Background:

Some time ago, business needs, in order to quickly let the parsing of Excel storage, so the not very sure of the field is set to the text.

Today we need to optimize the table structure, control the field length to the appropriate range, and try not to use the text type.

--Calculation length
Select LENGTH (CAST (fileName as CHAR)) from files;

Select MAX (LENGTH (CAST (fileName as CHAR)) from files;

Select LENGTH (CAST (fileName as CHAR)) as Le from files have le > 20;

--try to convert to varchar type for calculation, but error, perhaps because varchar is longer, it is unreasonable to calculate length on a variable length type.

--This is not very clear, and so later when the more refined time to explain it.

Select LENGTH (CAST (fileName as VARCHAR)) from files;

[Sql]select LENGTH (CAST (fileName as VARCHAR)) from files;
[ERR] 1064-you has an error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use near ' VARCHAR ') from files ' at Line 1

"MySQL" calculates the length of the text type field using the length and cast functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.