SQL Server modifies two scenarios for replacing Text,ntext type fields

Source: Internet
Author: User
Tags truncated

Scenario one with update and replace

--Replacement statement (because the maximum value of varchar (max) is 8000, so a portion greater than 8000 is truncated)UPDATEDbo. Sns_userblogSET [Description]=REPLACE(CAST([Description]  as VARCHAR(Max)),'SX',"')WHEREBlogID=142

This scheme has some limitations, that is, the length of the field must not exceed 8000.

Scenario Two UpdateText method

--UPDATE statementDECLARE @ptrval BINARY( -)  SELECT @ptrval=TEXTPTR([Description]) fromSns_userblogWHEREBlogID=142IF @ptrval  is  not NULLUPDATETEXTSns_userblog.[Description] @ptrval NULL NULL 'Hellotheword SX'--Replacement statement (because the maximum value of varchar (max) is 8000, so a portion greater than 8000 is truncated)UPDATEDbo. Sns_userblogSET [Description]=REPLACE(CAST([Description]  as VARCHAR(Max)),'SX',"')WHEREBlogID=142

The method of UPDATETEXT parameter expansion in three-way scheme

--substitution StatementsDECLARE @ptr binary( -)--text PointerDECLARE @oldstr VARCHAR( -)Declare @dl INT--The length of the string that needs to be replacedDECLARE @val VARCHAR( -)--new string after replacementDECLARE @index INT --the start position to insertSet @oldstr = 'SX'--the string to replaceSet @dl = Len(@oldstr)--string block lengthSELECT @ptr=TEXTPTR([Description]),@val="',@index=CHARINDEX(@oldstr,[Description])-1  fromdbo. Sns_userblogWHEREBlogID=142--Query CriteriaUPDATETEXTDbo. Sns_userblog.description@ptr @index @dl @val

SQL Server modifies two scenarios for replacing Text,ntext type fields

Related Article

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.