Character length or Byte Length

Source: Internet
Author: User
Tags truncated

I encountered a small detail problem today and found it to be a long time.

String STR = "a.2104697aa (Wuhu jinaorui) B .2104697ba (Changsha Bosch )";

Str. Length = 40

In C #, the length of string STR is 40. Here it refers to the length of string.

If STR varchar (50) is inserted into the SQL database, the following error occurs: string or binary data wocould be truncated. If the string length is too long, it must be truncated.

Then, the result of the select Len ('A. 2104697aa (Wuhu jiangrui) B .2104697ba (Changsha Bosch) ') query is 40, which indicates that the length is not exceeded.

After searching, we found that the query result of select datalength ('A. 2379697aa (Wuhu jinaorui) B .2379697ba (Changsha Bosch) 'is 52.

Datalength: returns the length of the string in bytes.
Len: returns the length of characters.

The length of varchar refers to the length of the string in bytes.

Appendix: differences between varchar nvarchar char

From: http://www.cnblogs.com/yelaiju/archive/2010/05/29/1746826.html

Character length or Byte Length

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.