SQL-character data (varchar)

Source: Internet
Author: User

I just considered whether to write every day! However, it is time to continue. Otherwise, you will not be able to live well. There are old and small ones. Besides, writeArticleLong memory! Improve yourself! :)

Okay, no nonsense! I checked the email and received the newsletter from sqlservercentral.com. There is a problem in this issue:

 Declare @ Str Varchar ( Max ) Select @ STR = replicate ( '#' , 10000) + replicate ( Cast ( '#'   As   Varchar ( Max ), 8000) + '#' +'#' + '#'   Select Len (@ Str) Output   Of The above code In   SQL Servers 2005 Is : A. 16003b. 18005c. 8000d. Error: incorrect syntax near '.

The data type varchar may not be used much, but it is even more unclear about varchar (max. As a result, I chose C, Sorry-you were wrong.

In my understanding, the length of varchar in SQL Server 2005 is only 8000 bytes, and the maximum length is 8000 bytes. So I chose C, a wrong answer.

It turns out that varchar can accommodate up to 2,147,483,648 bytes in varchar (max), which is the case in SQL Server 2005. In Oracle 9i, varchar has only 4000 bytes. In MySQL, there are 65,535 bytes.

So will the answer be B? No, B is also wrong. It won't be D, or at least those SQL statements won't go wrong.

The correct answer is.

The reason is: although the replicate function can replicate specified characters (strings) for a certain number of times according to your ideas, it is not infinite) if the data type of is varchar (max) or nvarchar (max), replicate will get only one 8000 bytes value, and will be automatically truncated when the data type is exceeded.

In this way, the first replicate ('#', 10000) can only get 8000 '#', while the second one explicitly converts '#' to varchar (max ), however, because 8000 is specified, there are only 8000 '#'.

With the last three '#' added, the result of the final Len function is 16003.

 

Although the purpose of this problem is the replicate function, varchar (max) is also in it ).

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.