Stuff and Replace Function in SQL Server : Detailed Description

(This is a guest blog post by Daniel Jones (li | t | fb). Daniel is a SQL Server DBA and contributor at SQL Tech Tips. Having 2 + years of experience in SQL recovery and system infrastructure.)

In SQL Server, STUFF and REPLACE functions are used to replace the characters in a string. Both functions play an important role in Transact-SQL with performing the distinct functionality. In the following section, we are going to discuss about Stuff and Replace function in detail.

STUFF Function in SQL Server

In SQL Server, the STUFF function is utilized to replace the part of the string with some another string. Moreover it can remove the specified length of the characters in the first string and after that, insert the second string into the first string at the starting position.

Syntax of STUFF Function

STUFF ( character_expression , start , length , replaceWith_expression )


    • character_expression

This argument can be a constant, variable or may be the column of either binary or character data

    • start

It is an integer value that indicates the location from where to begin the deletion and insertion as well. In case, if start or length parameters may be negative then, it will return a null string. Besides, if start is used longer than the first-character_expression, again null string is returned. It also can be of bigint type.

    • length

Also, it is an integer that denotes the number of characters to remove. If using length longer than the first character_expression, then, deletion happens to the last character within the last character_expression. It could be of type bigint.

    • replaceWith_expression

This attribute represents same as character_expression. Even it may be a variable, constant, or columns of character or binary data. Even this expression can replace length characters from character_expression in the beginning at start

Return Types

In case, if character_expression is one of the supported character data, returns the character type data. Otherwise, gives binary data if character_expression is a single supported binary data types.


In the above parameters, if the length or start integer is negative, or if the start position is larger than the length of the primary string, then, returns the null string. Although, returns null value if the starting position is zero. If the length is longer than the first string, it has decided to remove the first character data type in the first string.

Replace Function in SQL Server

As the above function name indicates, the REPLACE function in SQL server allows to replace all the occurrences of a specified string value with another set of the string value.

Syntax of REPLACE Function

REPLACE ( string_expression , string_pattern , string_replacement )


    • string_expression

This string expression to be searched. Either it can be of character data or binary data type.

    • string_pattern

It can also be of a binary as well as character data type. In fact, it cannot be an empty string and may not exceed the maximum number of bytes that is suitable on a page.

    • string_replacement

A string_replacement argument can be of any data type either character or binary as well.

Return Types

If one of the input parameters is of nvarchar type then, returns nvarchar value; else, REPLACE gives varchar value.

In addition, if any individual argument is NULL then, returns the NULL value.

Moreover, if string-expression cannot be of type varchar or nvarchar, then, REPLACE function truncates the return range at 8,000 bytes. To provide values larger than 8,000 bytes, even string-expression may be cast explicitly to a greater value data type.


REPLACE function performs the comparison of the input, which is based on collation. In a specified collation performing a comparison, also users can use COLLATE to apply collation to the desired input.

Char(zero) is an undefined character in collation and unable to include in REPLACE function.


STUFF and REPLACE are two major SQL Server functions that have discussed in above section. Moreover, to understand the various aspects of both the functions, we have presented the syntax and parameters in detail.

Error 3241 in SQL Server

(This is a guest blog post by Daniel Jones (li | t | fb). Daniel is a SQL Server DBA and contributor at SQL Tech Tips. Having 2 + years of experience in SQL recovery and system infrastructure.)

Microsoft SQL Server Backup Error Code 3241 Restore Headeronly

Sometimes, the users of MS SQL server faced an error when they try to restore SQL database from the backup file in the SQL server. This error is known as SQL server error 3241 and it generates the below-mentioned error message.

Error 3241 SQL Server

Due to this error message, the users cannot restore the data from the backup file into SQL Server database. Therefore, in this post, we are going to discuss the reason for the occurrence of error 3241 and the relevant solutions to resolve the error.

Causes Behind the Occurrence Of the SQL Server Backup Error 3241

There are various reasons behind this error message. Some of them are described as below:

  • When a user of MS SQL Server tries to restore the data from the backup file that was created on the MS SQL Server 2014 to the SQL Server 2008 then the error message arise. This is the main cause behind the occurrence of this error.
  • The SQL error code 3241 may also occur if SQL Backup Agent Service does not have necessary access permission. To restore the database from backup file into the SQL server, the SQL Backup Agent Service should have read permission to access the folder, which contains the backup file.
  • There is also a possibility that the backup file, which was used for restoring the SQL database, is corrupted. The backup file can be corrupted if it was loaded using FTP Text Mode.

Solutions To Resolve the MS SQL Error 3241 Restore Headeronly

In this section, we are going to discuss some solution to troubleshoot the backup error 3241. These solutions are described as follows:

  • Before starting the process to restore a database from the backup file, we should check the version of SQL server. If the version of SQL server is same as that of the version in which the backup file was created then start the restore process, otherwise upgrade the SQL server database to the same or higher version.

Note: By running the command ‘SELECT @@Version’ in the SQL query window, you can check the version of SQL server.

  • There is a need for access permission to restore data from backup file using SQL Backup Agent Service. The user account, which was used to log on the SQL backup Agent service, should be specified while installing SQL server components on the server instance. SQL Backup Agent Service can get a permission to access some specific folder, where a backup file is saved from the sysadmin.
  • If the backup file was corrupted because of uploading using FTP text mode, then again restore the backup file using FTP in binary mode. If this method fails to repair the corrupted backup file then you can also take the help of SQL BAK file recovery, a third-party utility to recover backup file.

Note: To avoid the Microsoft SQL Server error code 3241, the version of SQL server should be same as that of SQL server version in which backup file is created. SQL Backup Agent Service should have necessary access permission to restore data from backup file and backup file should be upload using FTP in binary mode.


When the users try to restore the database from the backup file then sometimes they are unable to do that because of SQL server error 3241. There are various reasons behind the occurrence of this error such as older version of SQL server, backup file is uploaded using FTP binary mode, etc. Therefore, in this post, we have described the causes of the error and the possible solutions to fix the backup error code 3241.