Late Night Dev

Business and Development Resources for Web Developers and Analysts

The SQL STUFF Function

By Patrick Irvin • Feb 18th, 2009 • Category: SQL

The STUFF Function is used to replace one string in your data with another string.  It deletes a specified length of characters from a string beginning at a specified start position and replaces them with another string beginning at the start position.  The STUFF is very useful when you wish to only replace one specific string as opposed to the REPLACE function which replaces every occurence of the string in the data.

The syntax for STUFF is -

STUFF ( character_expression , start , length , replacement_characters )

The STUFF function can be used to accomplish many useful tasks, below is an example using STUFF to insert dashes into a social security number.

DECLARE @vSnum VARCHAR(11)

SELECT @vSnum =‘123456789′

SELECT @vSnum,STUFF(STUFF(@vSnum,4,0,‘-’),7,0,‘-’)

For more details on the STUFF function visit http://msdn.microsoft.com/en-us/library/ms188043.aspx.

Post to Twitter Tweet This Post

Tagged as:

Patrick Irvin is a Business Analyst working in the Tuscaloosa, Alabama area. Patrick works with .Net technologies as well as LAMP/PHP applications and has a wide range of experience in internet related applications.
Email this author | All posts by Patrick Irvin

Leave a Reply

You must be logged in to post a comment.