We have an extremely large nvarchar(max) field that contains html. Within this html is an img tag.

Example:

```
<img style="float:right" src="data:image/png;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/7gAOQW....

```

The length of this column is 1645151, although what is being replace is a bit less than this, but not a lot.

What we are trying to do, is a replace in SQL on the column:

```
declare @url varchar(50) = 'myimageurl';
UPDATE table SET field =
CAST(REPLACE(CAST(field as NVARCHAR(MAX)),@source,'@url') AS NVARCHAR(MAX))

```

Where @source, is the above image bytes as string, which are assigned to an nvarchar(max) variable before running the replace. and dest is the url of an image, rather than the images bytes as string.

Although I still get the message string or binary data would be truncated. 

Does anyone know if this is possible in SQL to replace strings as large as this.
['https://Stackoverflow.com/questions/38659730', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1192535/']
I had the same error, but on a different function. 

The fault was that my pattern has longer than my expression, which means that your search pattern will be truncated.

I hope this helps someone.

Also, make sure you put pattern and expression in the right location of your function.
Instead of doing the replace, can you rebuilt the entire field by parsing out the rest of the img tag?

Something like:

```
declare @Field nvarchar(max) = '<img style="float:right" src="data:image/png;base64,/9j/4AAQSkZJRgA....BAQEBLAEsAAD/7gAOQW" />'
declare @Source nvarchar(max) = 'data:image/png;base64,/9j/4AAQSkZJRgA....BAQEBLAEsAAD/7gAOQW'
declare @URL nvarchar(max) = 'www.img.img/img.png'
declare @Chars int = 20

select left(@Field,patindex('%' + left(@Source,@Chars) + '%', @Field) - 1) as HTMLStart
        ,@URL as ImgURL
        ,right(@Field,len(@Field) - patindex('%' + right(@Source,@Chars) + '%', @Field) - @Chars + 1) as HTMLEnd

```

If you were wanting to run this on a whole dataset at once, you would simply need to look for the `src="data:image/png;base64,` element and work backwards from there using a similar methodology to the above. Depends on how you are identifying which binary data to replace and what to replace it with.